The asynchronous programming specification Promises and A in Javascript

  • 2020-03-30 03:13:56
  • OfStack

In Javascript, asynchronous programming is gradually accepted by everyone. Previously, people usually implement it through callback nesting, setTimeout, setInterval, etc., the code looks very unintuitive, and it is difficult to understand the whole code logic quickly without looking at it. The asynchronous functions in Javascript are probably I/O functions (Ajax, postMessage, img load, script load, etc.), timing functions (setTimeout, setInterval), etc.

We are all familiar with this. In complex applications, we tend to nest multiple layers and even cause program exceptions because some steps are not completed. The simplest example is that if you inject a node into the DOM, you have to wait for the node to be injected before operating the node. If our code relies on data from third-party apis. There is no way to know the latency of an API response, and other parts of the application may be blocked until it returns a result. Promises offers a better solution to this problem, which is non-blocking and completely decoupled from the code.

So, I'm going to look at asynchronous programming in Javascript, and I'm going to start by recommending that you look at the relatively popular Promises/A specification.

Promises/A specification

Note: for understanding purposes, the description may differ from the Promises/A specification;

CommonJS's Promises/A specification, which simplifies asynchronous programming by specifying the API interface, makes our asynchronous logic code easier to understand.
The realization of the following Promises/A specification we call Promise object, Promise object one and only three states: unfulfilled (unfinished), fulfilled (completed), failed (failure/deny); When initial creation is unfulfilled (unfinished) state, state can only from the unfulfilled (unfinished) become fulfilled (completed), or unfulfilled (unfinished) into a failed (failure/deny). State once become fulfilled (completed) or failed (failure/rejected), state cannot be changed again.

The Promises/A specification provides A solution for describing the concept of latency (or future) in A program. The main idea is not to execute one method and then block the application and wait for the results to return before calling back the other methods, but to return a Promise object to satisfy the future listener. Fulfilled and failed state can be monitored. Promise registers callbacks by implementing a then interface to return a Promise object:

then(fulfilledHandler, errorHandler, progressHandler) ; 

The then interface is used to listen for different states of a Promise. FulfilledHandler listens for fulfilled (completed) status, errorHandler listens for failed (failure/reject) state, progressHandler listens for unfulfilled (unfinished) status. Promise not to force unfulfilled (unfinished) event listeners (for example, we know that the old version of jQuery (1.5, 1.6) Deferred is the realization of a Promise, but no implementation of unfulfilled (outstanding) status of listening to and fro adjustable progressHandler).

It is generally believed, then the interface returns a new Promise object, rather than the original Promise object, the new new Promise object can be understood as the original Promise a view of an object, it contains only the original Promise object is a set of methods, these methods can only observe the state of the original Promise object, and cannot be changed deferred object's internal state. This avoids conflicts between multiple callers, who can change the state of the new Promise object without affecting other callers.

In addition, Promise provides a transition between resolve (the implementation state from incomplete to complete) and reject (the implementation state from incomplete to reject or fail).

Send a picture to help you understand:

< img SRC = "border = 0 / / img.jbzj.com/file_images/article/201406/20146691602530.png? 20145691618 ">

With Promise, you can write asynchronous logic with a synchronous mind. In asynchronous functions, you cannot use try/catch to catch an exception, nor can you throw an exception. With Promise, we can explicitly define the errorHandler directly, which is equivalent to catching an exception.

Following are several libraries that follow the Promises/A specification, when, q, rsv.js, jQuery.Deferred, and more.


Related articles: