O'Reilly logo

Rediscovering JavaScript by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required