JavaScript Asynchronous Invocation Framework (Part 5-chained implementation) _javascript tips

Source: Internet
Author: User
Call Portal
A chained invocation has two entrances to the Async.go method and the Async.chain method, which are essentially consistent, except that the Async.chain method does not provide the initial parameters when invoked, and the Async.go method provides the initial parameters at the time of invocation and initiates the asynchronous call chain.
Copy Code code as follows:

Async.chain = function () {
var chain = new Async.operation ({chain:true});
return chain;
};
Async.go = function (initialargument) {
Return Async.chain (). Go (initialargument);
}

As we can see here, the chained call itself is also a async.operation, the Go method and next method required for the chained invocation are all extensions to the async.operation, and this extension is not difficult, as described in the next section.
Extension methods
As we all know, the callback functions added through the Addcallback method are executed one at a--at least for the synchronization function, so we can maintain the asynchronous call queue with this feature of async.operation, provided we support it with queues for asynchronous calls.
For asynchronous calls to queue support, we'll deal with it later, starting with the out-of-the-box Addcallback method and the yield method to extend the Go method and next method.
Copy Code code as follows:

This.go = function (initialargument) {
Return This.yield (initialargument);
}
This.next = function (nextfunction) {
Return This.addcallback (nextfunction);
};

In fact, the go method and next method directly call the yield method and the Addcallback method. The semantics of the Go method, like the yield method, pass a parameter to the Async.operation instance and start the call queue. Also, the semantic and Addcallback method of the next method adds a call to the end of the queue.
Asynchronous Queues
How do you make a queue that is supposed to support synchronization only become asynchronous? This requires the detection of the return of each call in the queue, and if the return type is async.operation, we know it is an asynchronous call, using a special method and so on before it executes.
Copy Code code 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 a Async.operation instance, we use its own Addcallback method to help us execute the remaining calls in the queue. To be exact, we construct a new call chain, transfer the rest of the queue to the new call chain, and then let the current asynchronous call start the new call chain in the callback.
There are also places where we need to make a few modifications to accommodate the new asynchronous call queue. For example, the state change of result, States, and completed is different in chained calls.
Summary
We have slightly modified the original async.operation, so that it supports the asynchronous call queue, the complete code look here: Support for chained calls of the asynchronous call framework async.operation.
Now that we have a powerful async.operation, we're going to see how we can put it into more common usage patterns.

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.