Promises Continued
Promises are asynchronous: each promise starts when the previous succeeds and uses the previous promise's result.
To see this in action, let's log a message before and after we call the promise.
What do you think the order of output will be? 1. before Christmas 2. Salutations fellow child I enjoy interacting with! I notice you received a posable plastic Batman figurine during the Yultide season. What do you think of my new HasMattelbro Turbo-Man action figure? 3. after opening gifts
You would think this is the correct order, but you would be wrong. It's actually:
before Christmas
after opening gifts
Salutations fellow child I enjoy interacting with! I notice you received a posable plastic Batman figurine during the Yultide season. What do you think of my new HasMattelbro Turbo-Man action figure?
If you think about it, it does make sense. You wouldn't play with your new action figures before Christmas. You haven't received them yet!
This is where asynchronous programming fits in. The code will run without blocking or waiting for the result. Anything that needs to wait for a promise to proceed is put in .then
.
Last updated