In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what is Promise in js", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is Promise in js" this article.
What is Promise? can you implement it by hand?
Promise, translated as a promise, promises that it will give you a result over time. Programmatically speaking, Promise is a solution to asynchronous programming. The following is the description of Promise in MDN:
The Promise object is a proxy object (a proxy value), and the value of the proxied object may be unknown when the Promise object is created. It allows you to bind the corresponding handlers for the success and failure of an asynchronous operation. This allows asynchronous methods to return values like synchronous methods, but not immediately return the final execution result, but a promise object that represents future results.
A Promise has the following states:
Pending: the initial state, which is neither success nor failure.
Fulfilled: this means that the operation completed successfully.
Rejected: it means that the operation failed.
Once this commitment changes from a waiting state to another state, the state can never be changed, that is, once the state becomes fulfilled/rejected, it cannot be changed again. Maybe people don't understand Promise just by looking at the concept. Let's take a simple chestnut.
If I have a girlfriend, next Monday is her birthday, I promised her birthday a surprise, then from now on this commitment will enter the waiting state, waiting for next Monday, and then the state will change. If I surprise my girlfriend next Monday as promised, then the status of the promise will be changed from pending to fulfilled, indicating that the promise has been successfully fulfilled. Once this is the result, there will be no other results, that is, the status will not change. On the other hand, if I work overtime because I am too busy at work that day and forget about it, and the surprise is not fulfilled, the status will be changed from pending to rejected, and the time cannot be turned back, so the state can no longer be changed.
Last time we said that Promise can solve the problem of callback hell, yes, the Promise object in pending state will trigger the fulfilled/rejected state, and once the state changes, the then method of the Promise object will be called; otherwise, catch will be triggered. Let's rewrite the code for the previous callback to hell:
New Promise ((resolve Reject) = > {setTimeout (()) = > {console.log (1) resolve ()}, 1000)}). Then ((res) = > {setTimeout (()) = > {console.log (2)}, 2000)}) .then ((res) = > {setTimeout () = > {console.log (3)}) 3000)}) .catch ((err) = > {console.log (err)})
In fact, Promise also has some shortcomings, such as unable to cancel Promise, errors need to be caught through the callback function.
Promise handwritten implementation, interview sufficient version:
Function myPromise (constructor) {let self=this; self.status= "pending" / / define the initial state before the state change self.value=undefined;// defines the state when the state is resolved self.reason=undefined;// defines the state when the state is rejected function resolve (value) {/ / two = "pending", ensuring that the state change is irreversible if (self.status=== "pending") {self.value=value Self.status= "resolved";}} function reject (reason) {/ / two = "pending" to ensure that the state change is irreversible if (self.status=== "pending") {self.reason=reason; self.status= "rejected";}} / capture construction exception try {constructor (resolve,reject) } catch (e) {reject (e);}} / / defines the then method myPromise.prototype.then=function (onFullfilled,onRejected) {let self=this; switch (self.status) {case "resolved": onFullfilled (self.value); break; case "rejected": onRejected (self.reason); break Default:}} above is all the content of the article "what is Promise in js?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.