Promise
We mentioned in the previous chapters that Continuation Passing Style (CPS) is not the only way to write asynchronous code. In fact, the JavaScript ecosystem provides interesting alternatives to the traditional callback pattern. One of the most famous alternatives is promise, which is getting more and more attention, especially now that it is part of ECMAScript 2015 and has been natively available in Node.js since version 4.
What is a promise?
In very simple terms, promise is an abstraction that allows a function to return an object called promise
, which represents the eventual result of an asynchronous operation. In the promises jargon, we say that a promise is pending when the asynchronous operation is not yet complete, it's fulfilled when the operation successfully completes, and rejected when the operation terminates with an error. Once a promise is either fulfilled or rejected, it's considered settled.
To receive the fulfillment value or the error (reason) associated...