concept promise in category functional programming

This is an excerpt from Manning's book Functional Programming in JavaScript.
If you’re a jQuery user, you’ve probably interacted with promises already. jQuery’s $.getJSON operation (and any variation of the JQuery $.ajax calls) returns its own Deferred object (a nonstandard version of a Promise), which implements the Promise interface and has a then method. Hence, you can use Promise.resolve() to treat the Deferred object as a Promise:
Promise.resolve($.getJSON('/students')).then(function () ...);This object is now a thenable and used just like any promisified object. I chose to implement my own getJSON in listing 8.5 to illustrate the process of refactoring an API call to use promises.
Unlike the other monads you learned about in this book, promises know to “wait” for the long-running computation to complete before the mapped functions are run. In this manner, this data type tackles head-on the problem of latency present in asynchronous calls. Just like Maybe and Either document functions with uncertain return values, promises make the notion of waiting for data honest and transparent; they also have the benefit of providing a simpler alternative for executing, composing, and managing asynchronous operations when compared to traditional callback-based approaches.
You can use promises to wrap a value or a function to be processed in the future (if you have some Java experience, this is similar to the Future<V> object). A long-running operation can be a complex calculation, fetching data from a database or a server, reading a file, and so on. In the event of a failure, promises allow you to consolidate error-handling logic using approaches with a look and feel much like that used with Maybe and Either. In a similar fashion, a promise can provide information about the state of the work being done, so you can ask questions such as these: Has data been fetched successfully? And were there any errors during the operation?
As you can see in figure 8.5, at any point in time a promise can be in any of these states: pending, fulfilled, rejected, or settled. It begins with a status of pending (also called unresolved). Depending on the outcome of the long-running operation, the promise can move into either fulfilled (in case resolve is called) or rejected (in case reject is called). Once a promise has been fulfilled, it can notify other objects (continuations or callbacks) that its data has arrived; or, in the case of errors, it can invoke any failure callback function that you registered with it. At this point, the promise is said to be in the settled state.
Figure 8.5. How an async operation is wrapped in a Promise and provided with two callbacks: one for resolve and another for reject. The promise begins with a status of pending and then is either fulfilled or rejected, invoking the function resolve or reject, respectively, before moving into the settled state.
![]()
Promises allow you to reason about your programs more effectively and to cut through tangled and tightly coupled callbacks. Just as Maybe was used to eliminate the number of nested if-else conditions resulting from null-checks in your code, Promise can be used to convert a series of nested callback functions into a sequence of actions, similar to a monad’s map functor.