Javascript - Chaining of Promises

Introduction

Since .then() and .catch() from Promise return a new promise, you can chain promises together.

Chaining promises allows asynchronous operations to be chained together.

They are guaranteed to happen in the correct and expected order, resulting in code that looks almost synchronous.

Consider the following example:

Demo

const bond = new Promise((resolve, reject) => { 
    resolve("Bond"); 
}); /*w  w  w  . j av  a2  s  .com*/

bond.then((str) => `${str}, this is a test:${str}`) 
    .then((str) => `Hello, I'm ${str}!`) 
    .then((str) => console.log(str));

Result

Related Topic