Chaining – dealing with several promises

The most powerful feature of the promise lies in its ability to chain calls, thereby making code look synchronous. A chain looks like this:

getData()  .then(getMoreData)  .then(getEvenMoreData)  .catch(handleError)

This makes the code very easy to read. You are able to tell in which order things happen; namely, getData() followed by getMoreData(), followed by getEvenMoreData(). Not only are we able to run the methods in the order that we want, but we are also able to access the data from the previous promise, like so:

function getData() {  return new Promise((resolve, reject) => {    setTimeout(() => {      resolve('data');    })  })}function getMoreData(data) { // data is from getData return new Promise((resolve, ...

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.