JavaScript asynchronous call framework (Part 5-chained implementation)

Source: Internet
Author: User

Call entry
A chained call exists in Async. the go method and Async. the chain method has two portals, which are essentially the same, but Async. the chain method does not provide initial parameters when calling, but Async. the go method provides initial parameters for calling and starts asynchronous call chains.
Copy codeThe 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 and next methods required for chained calls are all in Async. operation the expansion above, and this expansion will not be very difficult, which will be described 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 the synchronous function, so we can use Async. this feature of Operation is used to maintain asynchronous call queues, provided that we provide queue support for asynchronous calls.
For Asynchronous calls to support queues, we will try again later. First, we will use the ready-made addCallback method and yield method to extend the go method and next method.
Copy codeThe Code is as follows:
This. go = function (initialArgument ){
Return this. yield (initialArgument );
}
This. next = function (nextFunction ){
Return this. addCallback (nextFunction );
};

In fact, the go and next Methods call the yield and addCallback methods directly. Similar to the yield method, the go method transmits a parameter to the Async. Operation instance and starts 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 we make a queue that only supports synchronization support asynchronous? This requires detection of the return of each call in the queue. If the return type is Async. Operation, we know that it is an asynchronous call, so that we can use a special method to wait until it is executed.
Copy codeThe 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 can use its own addCallback method to help us execute the remainder call in the queue. To be precise, we have constructed a new call chain and transferred all the remaining calls in the queue to the new call chain, then let the current asynchronous call start the new call chain in the callback.
In addition, we need to make slight changes to ensure compatibility with new asynchronous call queues. For example, status changes of result, state, and completed are different in chain calls.
Summary
The original Async. Operation is slightly modified to support asynchronous call queues. For the complete code, see Async. Operation.
Now we have a powerful Async. Operation. Next we will look at how to put it into more common use modes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.