遇到这样一个问题,打印结果是 0 1 2 3 4 5 6,我的理解是then中返回Promise会隐式调用一次 Promise的then在其中进行父级then的resolve,但是似乎这里调用了两次then。
Promise.resolve()
.then(() => {
console.log('0');
return new Promise(resolve => {
return resolve(4);
});
})
.then(res => {
console.log(res);
});
Promise.resolve()
.then(() => {
console.log('1');
})
.then(() => {
console.log('2');
})
.then(() => {
console.log('3');
})
.then(() => {
console.log('5');
})
.then(() => {
console.log('6');
});
给返回的Promise添加一个then,执行结果依然是 0 1 2 3 4 5 6
Promise.resolve()
.then(() => {
console.log('0');
return new Promise(resolve => {
return resolve(4);
}).then(() => 4);
})
.then(res => {
console.log(res);
});
Promise.resolve()
.then(() => {
console.log('1');
})
.then(() => {
console.log('2');
})
.then(() => {
console.log('3');
})
.then(() => {
console.log('5');
})
.then(() => {
console.log('6');
});
哪位大佬解答一下
用注释标注了下会产生promise的地方,对照下面描述查看,大概顺序是这样: