Nested Promise execution order
P粉459440991
2023-08-16 19:20:33
<p>I was reading about nested promises and came across this coding challenge in a tutorial. Can anyone explain the execution order of this code? </p>
<pre class="brush:php;toolbar:false;">new Promise((resolve) => {
new Promise((res) => {
console.log("c");
resolve(3);
res(2);
}).then((response) => console.log(response))
}).then((res) => console.log(res));</pre>
<p>I ran this code and the output was: </p>
<pre class="brush:php;toolbar:false;">c
2
3</pre>
<p>But I expected the output to be: </p>
<pre class="brush:php;toolbar:false;">c
3
2</pre>
<p>Because the outer promise is resolved first, and then the inner promise is resolved later. </p>
In addition to Nicholas' answer, you may be stuck by the repeated use of the identifier 'res'. The first use of a Promise internally is as a callback function, which returns the value 2 to the Promise. The second use in the last line is in a different scope and is used as a parameter name for the outer Promise's .then() method. It might help if you replace 'res' in the last line with 'outerPromiseResult', to clarify things.
In short, it's because of the order in which you call
.then
.In the above code, we enter the external constructor, which immediately calls the external function. Then create an internal Promise and call the internal function. The inner function records "c", then resolves the outer Promise to 3, then resolves the inner Promise to 2.
So at this point we have 2 resolved Promises, but no code trying to use them.
After completing the construction of the inner Promise, we call
.then
on the inner Promise. Since the Promise has resolved, this queues a microtask to run.After completing the construction of the external Promise, we call
.then
on the external Promise. Since the Promise has resolved, this queues a microtask to run.Now we have finished running all the synchronous code. The call stack is empty and the microtask starts running. These are executed in first-in, first-out order, so the microtasks related to the inner Promise run first, record 2. Then run the remaining microtasks and record 3.