In order to visualize how Promises execute, let’s define a new method delay(millisecs).
This is a utility method which will resolve once the timeout has passed.
The delay in milliseconds will be passed to .then’s callback.
Let’s look at 4 examples (with animated timelines).
This shows how console.log()’s execution will be delayed by delay(msec).
This shows a common mistake.
The console.log fires right when the delay(1000) begins. Not after the delay like you probably wanted.
Because console.log returns undefined our .then() is silently ignored.
Note the difference between typeof console.log === 'function' vs. typeof console.log() === undefined.
Generally the desired usage for console.log is shown in Example #1. Make sure you pass functions into .then and .catch.
Promise.all with 3 delay Promises. They will execute simultaneously.