Home >Web Front-end >JS Tutorial >JavaScript Asynchronous Calling Framework (Part 5 - Chain Implementation)_javascript skills

JavaScript Asynchronous Calling Framework (Part 5 - Chain Implementation)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:48:431062browse
Call entry
Chain call has two entries: Async.go method and Async.chain method. These two entries are essentially the same, except that the Async.chain method does not provide initialization when calling. Parameters, while the Async.go method provides initial parameters when called and starts the asynchronous call chain.
Copy code The code is as follows:

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 chain call itself is also an Async.Operation. The go method and next method required for the chain call are both on Async.Operation. extension, and this extension is not difficult, as will be explained in the next section.
Extension method
We all know that the callback functions added through the addCallback method will be executed one by one, at least for synchronous functions, so we can use this feature of Async.Operation to maintain the asynchronous call queue, provided that We added support for queuing asynchronous calls to it.
For asynchronous call queue support, we will deal with it later. First, we use the ready-made addCallback method and yield method to extend the go method and next method.
Copy code The code is as follows:

this.go = function(initialArgument) {
return this.yield(initialArgument);
}
this.next = function(nextFunction) {
return this.addCallback(nextFunction);
};

Actual Above, the go method and next method directly call the yield method and addCallback method. The semantics of the go method are the same as 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 also support asynchronous queue? This requires detecting the return of each call in the queue. If the return type is Async.Operation, we know that it is an asynchronous call, so we can use a special method to wait for it to complete before executing.
Copy code The code is as follows:

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 Async.Operation instance, we use its own addCallback method to help us execute the remaining calls in the queue. To be precise, we construct a new call chain, transfer the remaining calls in the queue to the new call chain, and then let the current asynchronous call start this new call chain in the callback.
In addition, there are some places where we need to make slight modifications to be compatible with the new asynchronous call queue. For example, the status changes of result, state, and completed are different in chain calls.
Summary
We have slightly modified the original Async.Operation to make it support asynchronous call queue. See the complete code here: Async.Operation, an asynchronous call framework that supports chain calls.
Now that we have a powerful Async.Operation, let’s see how to put it into more common usage patterns
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn