大家都知道jQuery的链式调用是return this的妙用,那么Promise的呢?比如
function start() { return new Promise((resolve, reject) => { resolve('start'); }); } start() .then(data => { // promise start console.log('result of start: ', data); return Promise.resolve(1); // p1 }) .then(data => { // promise p1 console.log('result of p1: ', data); return Promise.reject(2); // p2 }) .then(data => { // promise p2 console.log('result of p2: ', data); return Promise.resolve(3); // p3 }) .catch(ex => { // promise p3 console.log('ex: ', ex); return Promise.resolve(4); // p4 }) .then(data => { // promise p4 console.log('result of p4: ', data); });
result of start: start
result of p1: 1
ex: 2
result of p4: 4
第三步,reject的还被下一步的then忽略掉了,这种链式调用是什么原理?怎么实现的?
蛊毒传说
相关分类