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"); 
}); /*from  ww w .  j  a  v  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