I had a look at the first example of JavaScript promise on the promise page on MDN. After I clicked 3 times fast on the Make a promise!
button, I got the following result:
1) Started (Sync code started)
1) Promise started (Async code started)
1) Promise made (Sync code terminated)
2) Started (Sync code started)
2) Promise started (Async code started)
2) Promise made (Sync code terminated)
3) Started (Sync code started)
3) Promise started (Async code started)
3) Promise made (Sync code terminated)
2) Promise fulfilled (Async code terminated)
1) Promise fulfilled (Async code terminated)
3) Promise fulfilled (Async code terminated)
I totally understand that the lines Promise fulfilled
can be inserted anywhere between the blocks of promise starting output (depending when the promise value actually becomes available), I don't understand why the Promise started
lines are executed in sequence with Started
and Promise made
lines.
I would have expected an event to be place on the event queue, and the function passed to create a promise to be evaluated in its own context, after the Promise made
instruction has completed.
It seems I missed something fundamental about promises, but I can't pinpoint what exactly.