有两个情况,都返回Promise,并与以下then方法链接。但结果序列是不同的。 一些注释: Promise.resolve(value) -> 立即返回使用值实现的Promise。 并且在then方法中,当我们再次返回值时,它会返回一个带有该值的已完成Promise。 从逻辑上讲不应该有任何区别。两者都是立即... 提前感谢...
Promise.resolve(1)
.then((v) => {
console.log(v);
return Promise.resolve(v + 1);
})
.then((v) => {
console.log(v);
return Promise.resolve(v + 1);
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
});
Promise.resolve(10)
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
});
//Result on the console:
//1
//10
//11
//12
//2
//13
//3
//4
//
Promise.resolve(1)
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
});
Promise.resolve(10)
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
})
.then((v) => {
console.log(v);
return v + 1;
});
//Result on the console:
//1
//10
//2
//11
//3
//12
//4
//13