In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is promise in es6 and what is its function". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is promise in es6 and what is it?"
In es6, promise is used to create a new Promise object and generate Promise instances; promise is a constructor, a solution for asynchronous programming; and simply a container that holds the results of an event that will end in the future.
This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.
What is the use of promise in es6
Promise is an asynchronous programming solution provided in ES6, and Promise itself is a constructor.
Typeof Promise / / function
In general, we will use new Promise () to call the constructor to create a new Promise object. The Promise object has two characteristics.
1. The state of the object is not affected by the outside world.
The Promise object is an asynchronous operation with three states: pending (in progress), fulfilled (successful), and rejected (failed).
Only the result of asynchronous operation can determine which state Promise is, and no other operation can change this state.
2. Once the state of Promise changes, there will be no change, and this result can be obtained at any time.
There are only two ways to change the state of a Promise object: from pending to fulfilled or from pending to rejected. As long as these two situations occur, the state will not change again, and the result will be maintained all the time, which is called resolved. If the change has already happened, you will get this result immediately if you add a callback function to the Promise object. This is different from the Event, which is characterized by the fact that if you miss it and listen again, you won't get a result.
The advantage of using Promise is that when dealing with asynchronous programs, asynchronous operations are queued, executed in the desired order, and the expected results are returned, so that even multiple asynchronous operations can easily use Promise to make chain calls.
3. Promise also has some shortcomings.
First of all, you cannot cancel Promise. Once you create it, it will be executed immediately, and you cannot cancel it halfway. Second, if the callback function is not set, the errors thrown inside the Promise will not be reflected externally. Third, when in the pending state, there is no way to know what stage of progress it has reached (just started or is about to be completed)
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.
The following code creates an instance of Promise.
Const promise = new Promise (function (resolve, reject) {/ /... Some code if (/ * Asynchronous operation succeeded * /) {resolve (value);} else {reject (error);}})
The Promise constructor takes a function as an argument whose arguments are resolve and reject. They are two functions that are provided by the JavaScript engine and do not have to be deployed yourself.
The resolve function changes the state of the Promise object from "incomplete" to "successful" (that is, from pending to resolved), calls it when the asynchronous operation is successful, and passes the result of the asynchronous operation as an argument. The reject function changes the state of the Promise object from "incomplete" to "failed" (that is, from pending to rejected), calls when the asynchronous operation fails, and passes the error reported by the asynchronous operation as an argument.
After the Promise instance is generated, you can use the then method to specify the callback functions for resolved state and rejected state, respectively.
Promise.then (function (value) {/ / success}, function (error) {/ / failure})
The then method can take two callback functions as arguments. The first callback function is called when the state of the Promise object changes to resolved, and the second callback function is called when the state of the Promise object changes to rejected. The second function is optional and does not have to be provided. Both functions take values from the Promise object as arguments.
Thank you for your reading, the above is the content of "what is promise and what is the role of promise in es6". After the study of this article, I believe you have a deeper understanding of what promise is and what role it plays in es6, 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.
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.