Summary: Chaining Promises

Get a quick summary of chaining promises.

Promise chains

Multiple promises can be chained together in a variety of ways to pass information between them. Each call to then(), catch(), and finally() creates and returns a new promise that is resolved when the preceding promise is settled. If the promise handler returns a value, then that value becomes the value of the newly created promise from then() and catch() (finally() ignores this value). If the promise handler throws an error, then the error is caught and the returned, newly created promise is rejected using that error as the reason.

Handling rejections in promise chains

When one promise is rejected in a chain, the promises created from other chained handlers are also rejected until the end of the chain is reached. Knowing this, it’s recommended to attach a rejection handler at the end of each promise chain to ensure that errors are handled correctly. Failing to catch a promise rejection will result in a message being output to the console, an error being thrown, or both (depending on the runtime environment).

Returning promises from handlers

We can return promises from handlers, and in that case, the promise returned from the call to then() and catch() will settle to match the settlement state and value of the promise returned from the handler (fulfilled promises returned from finally() are ignored, while rejected promises are honored). We can use this to our advantage by delaying some operations until a promise is fulfilled, then initiating and returning a second promise to continue using the same promise chain.

Get hands-on with 1200+ tech skills courses.