Promise.then

1、If onFulfilled returns a promise, the return value of then will be resolved/rejected by the promise.

  如果then的handler中返回一个promise(叫A),那么then()所返回的promise(叫B)的值,将会是这A的值。

function resolveLater(resolve, reject) {
setTimeout(function () {
resolve(10);
}, 1000);
}
function rejectLater(resolve, reject) {
setTimeout(function () {
reject(20);
}, 1000);
} var p1 = Promise.resolve('foo');
var p2 = p1.then(function() {
// Return promise here, that will be resolved to 10 after 1 second
return new Promise(resolveLater);
});
p2.then(function(v) {
console.log('resolved', v); // "resolved", 10
}, function(e) {
// not called
console.log('rejected', e);
}); var p3 = p1.then(function() {
// Return promise here, that will be rejected with 20 after 1 second
return new Promise(rejectLater);
});
p3.then(function(v) {
// not called
console.log('resolved', v);
}, function(e) {
console.log('rejected', e); // "rejected", 20
});

2、In practice, it is often desirable to catch rejected promises rather than use then's two case syntax, as demonstrated below.

  通过使用catch()来处理失败,而不是使用then()的第二个参数。

Promise.resolve()
.then( () => {
// Makes .then() return a rejected promise
throw 'Oh no!';
})
.catch( reason => {
console.error( 'onRejected function called: ', reason );
})
.then( () => {
console.log( "I am always called even if the prior then's promise rejects" );
});

3、catch()中如无异常,则返回的是一个resolved promise。

Promise.reject()
.then( () => 99, () => 42 ) // onRejected returns 42 which is wrapped in a resolving Promise
.then( solution => console.log( 'Resolved with ' + solution ) ); // Resolved with 42

4、通过throw或rejected Promise来返回一个异常。

Promise.resolve()
.then( () => {
// Makes .then() return a rejected promise
throw 'Oh no!';
})
.then( () => {
console.log( 'Not called.' );
}, reason => {
console.error( 'onRejected function called: ', reason );
});

5、当return一个value时,实际上返回的是 Promise.resolve(<value>)

var p2 = new Promise(function(resolve, reject) {
resolve(1);
}); p2.then(function(value) {
console.log(value); //
return value + 1;
}).then(function(value) {
console.log(value + '- This synchronous usage is virtually pointless'); // 2- This synchronous usage is virtually pointless
}); p2.then(function(value) {
console.log(value); //
});

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

Promise.then的更多相关文章

  1. Javascript - Promise学习笔记

    最近工作轻松了点,想起了以前总是看到的一个单词promise,于是耐心下来学习了一下.   一:Promise是什么?为什么会有这个东西? 首先说明,Promise是为了解决javascript异步编 ...

  2. 路由的Resolve机制(需要了解promise)

    angular的resovle机制,实际上是应用了promise,在进入特定的路由之前给我们一个做预处理的机会 1.在进入这个路由之前先懒加载对应的 .js $stateProvider .state ...

  3. angular2系列教程(七)Injectable、Promise、Interface、使用服务

    今天我们要讲的ng2的service这个概念,和ng1一样,service通常用于发送http请求,但其实你可以在里面封装任何你想封装的方法,有时候控制器之间的通讯也是依靠service来完成的,让我 ...

  4. 闲话Promise机制

    Promise的诞生与Javascript中异步编程息息相关,js中异步编程主要指的是setTimout/setInterval.DOM事件机制.ajax,通过传入回调函数实现控制反转.异步编程为js ...

  5. 深入理解jQuery、Angular、node中的Promise

    最初遇到Promise是在jQuery中,在jQuery1.5版本中引入了Deferred Object,这个异步队列模块用于实现异步任务和回调函数的解耦.为ajax模块.队列模块.ready事件提供 ...

  6. Promise的前世今生和妙用技巧

    浏览器事件模型和回调机制 JavaScript作为单线程运行于浏览器之中,这是每本JavaScript教科书中都会被提到的.同时出于对UI线程操作的安全性考虑,JavaScript和UI线程也处于同一 ...

  7. JavaScript进阶之路——认识和使用Promise,重构你的Js代码

    一转眼,这2015年上半年就过去了,差不多一个月没有写博客了,"罪过罪过"啊~~.进入了七月份,也就意味着我们上半年苦逼的单身生活结束了,从此刻起,我们要打起十二分的精神,开始下半 ...

  8. 细说Promise

    一.前言 JavaScript是单线程的,固,一次只能执行一个任务,当有一个任务耗时很长时,后面的任务就必须等待.那么,有什么办法,可以解决这类问题呢?(抛开WebWorker不谈),那就是让代码异步 ...

  9. 浅谈Angular的 $q, defer, promise

    浅谈Angular的 $q, defer, promise 时间 2016-01-13 00:28:00  博客园-原创精华区 原文  http://www.cnblogs.com/big-snow/ ...

  10. angular学习笔记(二十八-附2)-$http,$resource中的promise对象

    下面这种promise的用法,我从第一篇$http笔记到$resource笔记中,一直都有用到: HttpREST.factory('cardResource',function($resource) ...

随机推荐

  1. 【Selenium-WebDriver自学】出现的问题和解决方案(十七)

    ==================================================================================================== ...

  2. QT学习之QT5.7+opencv3.1安装及显示图像

    如果有时间就按照这篇博文一步一步走: http://www.cnblogs.com/howlclat/p/6433097.html, 如果没时间: 直接下载最后的文件就可以了,不要浪费时间再去编译,真 ...

  3. express总结(一)

    Express 框架核心特性: 可以设置中间件来响应 HTTP 请求. 定义了路由表用于执行不同的 HTTP 请求动作. 可以通过向模板传递参数来动态渲染 HTML 页面. express保留了Nod ...

  4. fabric默认样例的分析

    参考资料 http://www.bubuko.com/infodetail-2092748.html http://www.ithao123.cn/content-11117437.html http ...

  5. Ubuntu的常用快捷键(摘自网络)

    篇一 : Ubuntu的复制粘贴操作及常用快捷键(摘自网络) Ubuntu的复制粘贴操作 1.最为简单,最为常用的应该是鼠标右键操作了,可以选中文件,字符等,右键鼠标,复制,到目的地右键鼠标,粘贴就结 ...

  6. 20165304 实验二 Java面向对象程序设计

    一.面向对象程序设计1--单元测试和TDD 实验要求 1.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 2 ...

  7. tips___代码规范

    函数变量尽可能置于最小作用域内,并在变量声明时进行初始化 变量声明的位置最好离第一次使用的位置越近越好:应使用初始化的方式代替声明再赋值. int x=0; rather than  int x; x ...

  8. C语言基础入门

    搭建Windows平台C/C++开发环境 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步: 第10步: 第11步: 第12步: 第13步: 第14步: 第15步 ...

  9. 对于两个初始时设置为Sensor的刚体,不会触发preSolve和postSolve

    Main.as package{ import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamic ...

  10. linux 一个跟踪文件删除的小技巧

    最近有同事问我说他有个现场环境,经常会丢失业务文件,每天都出现,几百个里面丢失1到两个. 为了解决这个问题,我让他布置audit,具体可以man一下auditctl. 过了一天,他说audit.log ...