What are Promise objects?

Js a new solution for asynchronous programming, used to represent the final completion (or failure) of an asynchronous operation, and its result value. Syntactically: A promise is a constructor. Simply put, a Promise object encapsulates an asynchronous operation and can retrieve its result

Grammar:

new Promise(function (resolve, reject) {
    ...    
} /* executor */)Copy the code

Executor: Executor is a function that takes resolve and reject. The Executor function is called immediately upon execution of the Promise constructor, and resolve and reject are passed to the executor as arguments (the executor function is called before the Promise constructor returns the constructed Promise instance object). The resolve and reject functions, when called, change the state of the PROMISE, respectively

Fulfilled,
fulfilled

reject


Promises come in three states

Pending (An initial state that is neither successful nor failed.) “Resolved”, which is a pity, Rejected.

  1. There are only two ways in which these three states can change:
  2. Asynchronous operation is never Completed Pending => Completed Resolved
  3. The asynchronous operation never completes pending => Failed Failed
  4. Once a state changes, it cannot be changed again. That’s where its name comes from. A promise object can only change once

A Promise object in the pending state may become a pity state and pass a value to the corresponding state processing method, or become a Promise object in the Rejected state and pass failure information. When either of these conditions occurs, the Handlers bound to the Promise object’s THEN method are called. (The THEN method contains two parameters: onfulfilled and onRejected, which are both Function types.) When the Promise state is

fulfilled
rejected

Because the promise.prototype. then and promise.prototype. catch methods return a Promise object, they can be called chained.