In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how JavaScript invokes the framework asynchronously". The content 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 "how to call the framework asynchronously with JavaScript".
Call entry
There are two entries for chain call: the Async.go method and the Async.chain method, which are essentially the same, except that the Async.chain method does not provide the initial parameters while the Async.go method provides the initial parameters and starts the asynchronous call chain.
Async.chain = function () {var chain = new Async.Operation ({chain: true}); return chain;}; Async.go = function (initialArgument) {return Async.chain (). Go (initialArgument);}
Here we can see that the chained call itself is an Async.Operation, and the go and next methods required for chained calls are extended on top of Async.Operation, and this extension will not be very difficult, which will be explained in the next section.
Expansion method
We all know that callback functions added through the addCallback method are executed one by one, at least for synchronous functions, so we can use this feature of Async.Operation to maintain the asynchronous call queue, as long as we add queue support for asynchronous calls.
For the support of queuing for asynchronous calls, we will deal with it later. First, we extend the go method and the next method using off-the-shelf addCallback methods and yield methods.
This.go = function (initialArgument) {return this.yield (initialArgument);} this.next = function (nextFunction) {return this.addCallback (nextFunction);}
In fact, it is the yield and addCallback methods that are called directly by the go and next methods. The semantics of the go method is the same as that of the yield method, passing a parameter to the Async.Operation instance and starting the call queue. At the same time, the semantics of the next method and the addCallback method add a call to the end of the queue.
Asynchronous queue
How can a queue that originally only supports synchronization become asynchronous as well? This requires checking the return of each call in the queue. If the return type is Async.Operation, we know that it is an asynchronous JavaScript call, so we use a special method to wait for it to be executed.
CallbackResult = callback (self.result); self.result = callbackResult; if (callbackResult & & callbackResult instanceof Async.Operation) {innerChain = Async.chain (); while (callbackQueue.length > 0) {innerChain.next (callbackQueue.shift ());} innerChain.next (function (result) {self.result = result; self.state = "completed"; self.completed = true; return result;}) CallbackResult.addCallback (function (result) {self.result = result; innerChain.go (result);});}
If the call returns an instance of Async.Operation, we use its own addCallback method to help us execute the remaining calls in the queue. To be exact, we constructed a new call chain, transferred the rest of the queue calls to the new call chain, and then let the current asynchronous call start the new call chain in the callback.
In addition, there are some areas that we need to modify slightly to be compatible with the new asynchronous call queue. For example, the state changes of result, state, and completed are different in chained calls.
Thank you for reading, the above is the content of "how JavaScript invokes the framework asynchronously". After the study of this article, I believe you have a deeper understanding of how JavaScript invokes the framework asynchronously, and the specific usage 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.