What happens if a promise completes before then is called?

前端 未结 4 652
花落未央
花落未央 2021-01-01 11:51

Let\'s say I have a Promise like this:

var promise = new Promise(function(resolve, reject) {
    // Do some async thing
});
promise.then(functio         


        
4条回答
  •  灰色年华
    2021-01-01 12:04

    As expected: then callback will get called immediately in this case if then was called after promise has already resolved.

    It's easy to test:

    var promise = new Promise(function(resolve, reject) {
        resolve(123);
    });
    
    setTimeout(function() {
      promise.then(function(response) {
          alert(response);
      });
    }, 1000)

提交回复
热议问题