In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
 
            
                     
                
2025-10-31 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 ES6's Promise.all implementation at least for how long". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!
1. Background
We all know that a timeout can be added to an ajax request, that is, the maximum amount of time required for the request. If it exceeds this time, an error will be reported directly. This is how long it takes to request at most, and what I need to do now is at least how long it takes to request before I can execute the subsequent logic.
For example, if an ajax request is executed in x milliseconds, but I want him to execute it for at least one second, we will think of it this way: after the ajax is completed, 1. If x 1s-x, then perform subsequent actions. 2 if x > = 1s, then we directly perform the follow-up operation. Think about it. We have to record the start time before and the end time later before we can get X. no, no, no. no, no, no.
Or it is very tedious to complete setting flag,setTimeout, setting flag and so on in variable flag,ajax.
2 、 Solution
Now ES6 has a Promise.all, which is very suitable for solving such problems. Directly Promise.all ([ajaxPromise (), waitPromise (1s)]. Then (() = > at least 1 s).
What if multiple ajax (promise) are executed sequentially, but the total time is at least 1 second? Then wrap multiple ajax in a single Promise. Then Promise.all ([ajaxPromiseAll (), waitPromise (1s)]) .then (() = > executed for at least 1s).
3. It is very abstract, as evidenced by examples.
This example is like this. There are red packets in Wechat. When we click to open, the word will at least be flipped through completely. We assume that it will take one second to double the whole time. If we immediately ask ajax when we click open, and open the red packet immediately when ajax is finished, the time here may be less than 1 second, so we won't be able to flip through it completely. If the request is greater than 1s, let it flip until the request is completed. So in order to solve this problem, we need to use the above technology.
The reference code is as follows (tested under the latest version of Chrome):
/ ajax Simulation Aconst funcA = async () = > new Promise (resovel = > {setTimeout () = > {console.log ("done A"); resovel ("funcA");}, 400);})
Because async and await are better to use than Promise, I use these two syntax sugars to write, using setTimeout to simulate ajax requests, ajax to simulate An and ajax to simulate B in order, such as checking whether this person still has a chance to open the red packet, and then asking to open the red packet to get the random red packet amount.
This is the end of the content of "how to use ES6's Promise.all implementation to request at least how long". 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about

The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r


A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from

Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





 
             
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.