In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the Promise of ES6". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
A Promise is simply a container that holds the result of an event (usually an asynchronous operation) that ends in the future. ES6 specifies that the Promise object is a constructor that is used to generate Promise instances.
Overview
Is a solution to asynchronous programming.
Syntactically, Promise is an object from which you can get messages for asynchronous operations.
The characteristics of Promise 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 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)).
This is the end of the content of "how to use the Promise of ES6". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.