* preface:
In the past, when you were learning JavaScript for the first time, it was difficult to think about functions. In some ways, the application of function objects really brings a lot of convenience to code.
The execution environment of the JavaScript language is “single-threaded”, so imagine if there were no asynchronous operations in JavaScript, we wouldn’t be able to use it at all. Among them, the callback function is the original method of using it to solve the problem of asynchronous operation.
Callbacks are widely used, for example, to insert a callback into a piece of code, which means that the previously packaged “code block” is executed again somewhere. Above, callbacks can solve the problem of asynchronous programming. However, when too many nested callback functions result in accumulation of callback functions, our code will be extremely complex to maintain, and the nesting of multiple callback functions will make our code deviate from the design concept of “high cohesion, low coupling”, and affect the whole body.
The nesting of multiple layers of callback functions is called callback hell.
When there are too many nested layers of callback functions, instead of using callback functions to solve asynchronous operation problems, Promise is used.
In the ES6 language standard, a Promise is an object that represents an asynchronous operation, and it can get the message of the asynchronous operation to change the state of the Promise itself. The Promise object has three states:
1. Waiting pending 2. Failed reject 3.Copy the code
With the Promise object, we can express asynchronous actions synchronously. *
## Basic usage:
// As specified in ES6, the Promise object is a constructor that generates a Promise instance. Let promise = new promise (function(resolve,reject) {let promise = new promise (function(resolve,reject) { The Promise object takes a function as an argument that takes resolve and reject, which are also two functions. We call these two functions to switch the Promise state to the corresponding state. A call to resolve switches to the success state (passing the result of the asynchronous operation as an argument); Reject is a failed state. }) After the Promise instance is generated, we can use the then method to specify the Resolved and Rejected state callback functions, respectively. Promise. Then (function(data) {When a Promise switches to the FULFILL state, Console. log(' I will only be called when the state is fulfill ', data)}, function(error) { The second callback is optional. Console. log(' REJECT I'm called when I'm in reject ', error)}) is called when a promise switches to reject. Both callbacks take values from the Promise object. The Promise object is executed as soon as it is created, and the callbacks specified by the THEN method will not be executed until all synchronization tasks of the current script have been completed. Finally, let's talk about the downsides of Promise objects. 1: Once we create a Promise object, it cannot be cancelled, and it will execute immediately. 2: We must use the Promise in conjunction with the callback function, meaning that if we do not set the callback through the then method, the result of the Promise object will not be reflected externally. That's all I have to say about the initial concepts of promises, and I'll introduce you to other methods of Promise objects such as race, all, and so on in the next article.Copy the code