Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand ES6 Promise object

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand ES6 Promise objects". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's ideas to study and learn "how to understand ES6 Promise objects".

Overview

Is a solution to asynchronous programming.

Syntactically, Promise is an object from which you can get messages for asynchronous operations.

Promise statu

The characteristics of the state

There are three states for Promise asynchronous operations: pending (in progress), fulfilled (successful), and rejected (failed). Nothing can change this state except as a result of an asynchronous operation.

The Promise object only has a state change from pending to fulfilled and from pending to rejected. As long as you are in fulfilled and rejected, the state will not change again, that is, resolved.

Const p1 = new Promise (function (resolve,reject) {resolve ('success1'); resolve (' success2');}); const p2 = new Promise (function (resolve,reject) {resolve ('success3'); reject (' reject');}); p1.then (function (value) {console.log (value); / / success1}); p2.then (function (value) {console.log (value); / / success3})

Shortcomings of the state

Cannot cancel Promise, as soon as it is newly created, it will be executed immediately, and cannot be cancelled halfway.

If the callback function is not set, the errors thrown inside the Promise will not be reflected externally.

When you are in the pending state, there is no way to know what stage of progress you are currently at (just beginning or nearing completion).

Then method

The then method takes two functions as arguments, the first is the callback if the Promise executes successfully, and the second is the callback if the Promise fails, and only one of the two functions will be called.

Characteristics of then method

The callback function will never be called until the current run of the JavaScript event queue is complete.

Const p = new Promise (function (resolve,reject) {resolve ('success');}); p.then (function (value) {console.log (value);}); console.log (' first'); / / first// success

Callback functions added in the form of. Then are called at any time.

Through multiple calls

.then

You can add multiple callback functions that run independently in the insertion order

Const p = new Promise (function (resolve,reject) {resolve (1);}) .then (function (value) {/ / first then / / 1 console.log (value); return value * 2;}) .then (function (value) {/ / second then / / 2 console.log (value);}) .then (function (value) {/ / third then / / undefined console.log (value); return Promise.resolve ('resolve')) }) .then (function (value) {/ / fourth then / / resolve console.log (value); return Promise.reject ('reject');}). Then (function (value) {/ / Fifth then / / reject:reject console.log (' resolve:' + value);}, function (err) {console.log ('reject:' + err);})

The then method returns a Promise object with a resolved or rejected state for chained calls, and the value of the Promise object is the return value.

Attention points of then method

Simple Promise chain programming is best kept flat and not nested Promise.

Note that the Promise chain is always returned or terminated.

Const p1 = new Promise (function (resolve,reject) {resolve (1);}) .then (function (result) {p2 (result) .then (newResult = > p3 (newResult));}) .then (() = > p4 ())

When you create a new Promise but forget to return it, the corresponding chain is broken, causing p4 to occur at the same time as p2 and p3.

The rejection in the unterminable Promise chain in most browsers is recommended to be followed by .catch (error = > console.log (error)).

Thank you for your reading, the above is the content of "how to understand ES6 Promise objects", after the study of this article, I believe you have a deeper understanding of how to understand ES6 Promise objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report