Resolve order of Promises within Promises

前端 未结 2 1580
渐次进展
渐次进展 2021-01-14 06:40

For the below code

function inner () {
  new Promise(function(resolve,reject){
    resolve()
  }).then(function(){
    console.log(\'Inner Promise\')
  })
}
         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 07:10

    I thought the outer resolve would be the first to enter the JS Message Queue followed by the inner resolve.

    Yes, the "outer" promise is resolved first. Put a console.log right next to the resolve call.
    But no, the outer then callback is not put in the queue first because it installed after the inner then callback. What you are doing is essentially equivalent to

    var outer = Promise.resolve();
    var inner = Promise.resolve();
    inner.then(function() {
        console.log('Inner Promise')
    });
    outer.then(function(data) {
        console.log('Outer Promise')
    });
    

    but obfuscated due to the nested (synchronous) function calls.

提交回复
热议问题