代码
var A = new Promise(function (resolve, reject) {
console.log('do sth.');
setTimeout(() => {
console.log('~~~~~');
resolve('ok');
// reject('error ******');
console.log('+++++');
}, 3000); // 延时3秒执行一次结束
});
// var B = A.then() //穿透
var B = A.then(
//处理失败上一个Promise的成功,并返回自己生成的Promise的返回值(默认返回undefined resolve(2222))利用Promise.reject(reason)传给下个promise异常
value => {console.log(111, value);return Promise.reject(2222)},
reason => {console.log(222, reason);return 'A.then.error';}); //处理失败上一个Promise的失败,并返回自己生成的Promise的返回值
// setInterval(() => {
// console.log(A);
// console.log(B);
// console.log('~~~~~~~~~~~~~~~~~~~~');
// }, 1000);
// 捕获程序中的失败
let p3 = B.catch(
reason => console.log('main: ', reason)
)
console.log('===================');
效果
root@ubuntu:~/workspace/es6/babel# node dist/index.js
Debugger attached.
do sth.
===================
~~~~~
+++++
111 ok
main: 2222
Waiting for the debugger to disconnect...