总结:then中的代码是同步执行的即使是链式调用也是如此。但then中的回调函数一定要等待前面返回promise结果才能执行。
const a = new Promise((resolve, reject) => {
console.log(2);
}).then(function x() {
console.log(5);
}).then(console.log(111))
// // 2,111
const b = new Promise((resolve, reject) => {
resolve(1)
console.log(2);
}).then(function x() {
console.log(5);
}).then(console.log(111))
// // 2,111,5 //链式调用.then时并不是后面的then一定要等前面返回结果才会执行。在链式调用中.then也是遵循then中代码同步执行;
//需要注意的是:.then中如果是回调函数的话,一定需要等待前面返回promise状态后才能执行。
const c = new Promise((resolve, reject) => {
resolve(1)
console.log(2);
}).then(function x() {
console.log(5);
}).then(function x() {
console.log(111);
})
//2,5,111
const c = new Promise((resolve, reject) => {
console.log(2);
}).then(
console.log(5)
).then(function x() {
console.log(111);
})
//2,5