Promise handwritten promise from getting started to getting offer

Time:2021-12-31

1. Implementation of promise constructor

Promise constructor is used to declare the sample object, and an executor function needs to be passed in. It includes the resolve function and reject function, as well as several important attributes: status attribute, result attribute and callback function queue.

Promise handwritten promise from getting started to getting offer

Basic framework of constructor

The resolve function is used to asynchronously handle the functions invoked after success. This includes the number of times to modify the state of the verification object, modify the state of the promise instance object, and asynchronously call the successful callback function onresolved.

Promise handwritten promise from getting started to getting offer

Resolve function

The reject function is used to asynchronously handle functions that are called after failure. This includes the number of times to modify the state of the verification object, modify the state of the promise instance object, and asynchronously call the successful callback function onrejected.

Promise handwritten promise from getting started to getting offer

Reject function

2. Realization of promise’s then function and catch function

The then function is used to specify the success / failure callback function for promise, and the return value is a new promise object.

Promise handwritten promise from getting started to getting offer

Basic framework of then function

If onresolved / onrejected is not a function, it can specify a default function. If onresolved is not a function, specify that the returned promise is a success status and the result value is value; If onrejected is not a function, specify that the returned promise is a failed state and the result value is reason. Otherwise, a new promise object is returned.

Promise handwritten promise from getting started to getting offer

Otherwise, a new promise object is returned.

Promise handwritten promise from getting started to getting offer

Function used to process promise success / failure results.

Promise handwritten promise from getting started to getting offer

Promise handwritten promise from getting started to getting offer

The catch function is used to specify the failed callback function. It is the syntax sugar of then (null, onrejected).

Promise handwritten promise from getting started to getting offer

Catch function

3. Implementation of promise’s resolve function and reject function

Promise handwritten promise from getting started to getting offer

Resolve function
Promise handwritten promise from getting started to getting offer

Reject function

4. Implementation of promise’s all function, race function and any function

Must the passed in be a promise object array?Can be a constant.If one of them reports an error, it will enter the catch, and the other promises will be executed normally. They will be executed at the beginning of creation (instantiation). Then () only gets its results.

Promise handwritten promise from getting started to getting offer

Promise’s all function
Promise handwritten promise from getting started to getting offer

Race function
Promise handwritten promise from getting started to getting offer

Any function

Recommended Today

Could not get a resource from the pool when the springboot project starts redis; nested exception is io. lettuce. core.

resolvent: Find your redis installation path: Start redis server Exe After successful startup: Restart project resolution. ———————————————————————->Here’s the point:<——————————————————————- Here, if you close the redis command window, the project console will report an error. If you restart the project, the same error will be reported at the beginning, The reason is: It is inconvenient to […]