代码
var myPromise = new Promise(function (resolve, reject) {
console.log('do sth.');
setTimeout(() => {
console.log('~~~~~');
resolve('ok');
// reject('error');
}, 3000); // 延时3秒执行一次结束
});
let pro1 = myPromise.then(value => {
/*如果成功则显示结果*/
console.log(1, 'successful');
return 1111;
}, reason => {
/*如果失败则显示原因*/
console.log(2, 'failed');
return 2222;
});
let pro2 = myPromise.catch(reason => {
console.log(3, reason);
});
// 开始链式调用
pro2.then(value => console.log(4, value),
// value是什么?
reason => console.log(5, reason) // reason是什么?
).then(value => {
console.log(6, value); // 已经不是pro2对象了,value是什么
return Promise.reject('pro2 => new Promise object rejected');
}).catch(reason => {
console.log(7, reason);
return Promise.resolve(reason + ' *');
}).then(value => console.log(8, value),
// value是什么?
reason => console.log(9, reason) // reason是什么?
); // 返回的是什么?
结果
root@ubuntu:~/workspace/es6/babel# node dist/index.js
Debugger attached.
do sth.
~~~~~
1 successful
4 ok
6 undefined
7 pro2 => new Promise object rejected
8 pro2 => new Promise object rejected *
Waiting for the debugger to disconnect...