Chaining Promises
One elegant feature of promises is that they form a pipeline like in the functional composition we saw in Arrow Functions and Functional Style. Since both then() and catch() return a promise, calls to these functions may be chained to apply a series of filtering and transformations. An example will help you appreciate this elegance.
In the example in No Thanks to Callback Hell we used the fs library’s readFile() asynchronous method that relied on callback. The fs-extra library provides a wrapper around the functions of the fs library. We will use that to read the contents of a file asynchronously, but this time using promises instead of callbacks.
Get Rediscovering JavaScript 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.