Chaining Promises
Learn about the basics of the promise chain and find out the results of passing different arguments to the promise chain.
We'll cover the following...
One elegant feature of promises is that they form a pipeline like in the functional composition we discussed in the Arrow Functions and Functional Style lesson. Since both then()
and catch()
return a promise, calls to these functions may be chained to apply a series of filterings and transformations. An example will help you appreciate this elegance.
fs
library
In the example in No Thanks to Callback Hell, we used the fs
library’s readFile()
asynchronous method, which relied on callbacks. The fs-extra
library provides a wrapper around the functions of the fs
library. We will use this to read the contents of a file asynchronously, but this time, using promises instead of callbacks.
Wrapping Callbacks into Promises
fs-extra
is a library ...