Search⌘ K
AI Features

Fundamentals of Chaining Promises

Explore how to chain multiple JavaScript promises to handle asynchronous operations in sequence. Learn to manage errors centrally with catch handlers that act like try-catch statements, ensuring robust asynchronous code. This lesson helps you write cleaner and more maintainable promise-based code.

We'll cover the following...

Introduction

Promises may seem like little more than an incremental improvement over using some combination of a callback and the setTimeout() function, but there is much more to promises than meets the eye. More specifically, there are several ways to chain promises to accomplish more complex asynchronous behavior.

Chaining promises
Chaining promises

Each call to then(), catch(), or finally() actually creates and returns another promise. This second promise is settled only once the first has been fulfilled or rejected. Consider this example:

Javascript (babel-node)
const promise = Promise.resolve(66);
promise.then(value => {
console.log(value);
}).then(() => {
console.log("Finished");
});

The call to ...