关于promise.then执行顺序的问题

function testPromise4 () {
  let promise = new Promise(resolve => {
    resolve(1);
  });

  promise.then(res =>{
    console.log('第1个then,返回的值为:', res);
    return 2;
  })
    .then(res => {
      console.log('第2个then,返回的值为:', res);
      return 3
    });

  return promise;
}

testPromise4().then(res => {
  console.log('第3个then,返回的值为:', res);
});

上面这段代码我原本以为执行后输出的结果是:

  • 第1个then,返回的值为:1
  • 第2个then,返回的值为:2
  • 第3个then,返回的值为:3

但实际结果却是:

第1个then,返回的值为:1
第3个then,返回的值为:1
第2个then,返回的值为:2

哪位大佬知道这是为什么?

阅读 2.7k
1 个回答

每个promise.then都会产生一个新的promise,所以返回promise和返回promise.then是不一样的

推荐问题