How Promise.resolve to resolve “then object”?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How Promise.resolve to resolve “then object”?



This is my code:


var pr = Promise.resolve( {
then:function(resolve,reject) {
setTimeout( () => {
reject(new TypeError('myError'));
resolve('run');
},2000)
}
})



I don't know ,what is priority in run?



guesse 1 :



step1 : at first, setTimeout is executed and returns promise with result myError for then method.


setTimeout


promise


myError


then



step2 : then method returns same Promise for resolve method.


then


Promise


resolve



guesse 2 :



step1 : Promise.resolve( returns Promise that,its then method changes to:


Promise.resolve(


Promise


then


function(resolve,reject) {
setTimeout( () => {
reject(new TypeError('myError'));
resolve('run');
},2000)
}



step2 : Finally pr.then(function(....){}) produce result.


pr.then(function(....){})



I do not know how it works and what my guess is right.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results