In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to encapsulate a terminable Promise". 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!
Today, I was asked by a colleague how to stop the Promise call chain. According to the official document, the native Promise cannot be aborted, but we can make a small modification to encapsulate a Promsie that can be "aborted". The code is as follows: function MyPromise (callback) {/ / 1, define the variable let _ resolve; let _ reject; const promise = new Promise outside the Pormise ((resolve, reject) = > {/ / 2, assign the resolev and reject methods of promise to the external variable _ resolve = resolve; _ reject = reject; callback & & callback (resolve);})
Return {promise, _ reject}}
Let p1 = MyPromise (function (reslove) {setTimeout () = > {reslove (1000)}, 3000);}) let p2 = MyPromise (function (reslove) {setTimeout () = > {reslove (2000)}, 3000);}) let p3 = MyPromise (function (reslove) {setTimeout () = > {reslove (3000)}, 3000);})
P1.promise.then (data = > {console.log (data) return p2.offer;}) .then (data = > {console.log (data) return p3.offer;}) .then (data = > {console.log (data)}) .catch (e = > console.log (e))
/ / here p3 can be changed to p1 or p2, and the promise call chain p3._reject (444) can be terminated as needed.
Reading the code, we use closures to save the reject of each Promise. When we need to abort, we can "abort" the subsequent execution of the Promise by calling the reject of the corresponding Promise, which cleverly terminates the then chain execution of the Promise.
To sum up: when we use Promise, we usually think that the resolve and reject of Promise can only be executed within Promise, but we can get control of Promise through external variables by defining an external variable and then assigning reject or resove to the external variable when executing new Promise. Thus, the execution of the Promise chain is "suspended" at any time.
This is the end of "how to encapsulate a terminable Promise". Thank you for your 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.