Write a JavaScript Asynchronous call Framework (Part 2

Source: Internet
Author: User

In a previous article, we want to design an asynchronous invocation framework that is best able to unify the interfaces of the asynchronous invocation, while the exact invocation order is independent of the implementation. So now we're going to design a use case for such a framework.

Delivery callback

One of the first things we have to consider is how to pass the callback entry. In the most traditional xhr invocation, the callback function is passed as the last argument to the asynchronous function:

function asyncOperation(argument, callback)

When there are quite a few parameters, we can put the parameters in a JSON so that the parameters, like named parameters, can be passed by parameter names, and the parameters that are not passed are equivalent to using the default values. This is a popular approach from the beginning of prototype:

function asyncOperation(argument, options)

However, the disadvantage of both approaches is that when you change a synchronization function to an asynchronous function (or a synchronous asynchronous hybrid function), you must explicitly modify the function signature and add one (or more) arguments at the end.

Because it is too common for us to introduce asynchronous functions at the bottom of the call stack, it is too expensive to change the cost of a large stack of call function signatures, so let's think of a way to do this without modifying the function signature.

Here I refer to the IAsyncResult design of the. NET framework to centralize all the information about asynchronous operations into one object, thus avoiding the modification of the function signature. Here, we assume that the invocation prototype of an asynchronous function is this way:

function asyncOperation(argument) {
  operation = new Cat.Async.Operation();
  setTimeout(function() { operation.yield("hello world"); }, 1000);
  return operation;
}

In this code, we return a Operation object for future delivery of the callback function. At the same time, we simulate the asynchronous return result by settimeout, and the concrete return way is the yield method.

Next, we'll also design a way to pass the callback function. Because we cannot overload the + = operator as C #, you can only pass callback functions with functions:

var operation = asyncOperation(argument);
operation.addCallback(function(result) { alert(result); });

It is not safe to do such a design in C # because the asynchronous operation may be completed before the callback is added. But in JavaScript this is safe to write, because JavaScript is single-threaded, followed by AsyncOperation synchronization addcallback must first execute, asyncoperation in the asynchronous yield inevitable after implementation.

Call Order

One might ask if a user uses a synchronous method to invoke yield, does the order of execution depend on the implementation of yield? Yes, but Yeild is a one-time implementation in the framework, so just make it asynchronous, so that even if it's synchronized, it doesn't affect the order of execution:

function psudoAsyncOperation(argument) {
  operation = new Cat.Async.Operation();
  operation.yield("hello world");
  return operation;
}
var operation = asyncOperation(argument);
operation.addCallback(function(result) { alert(result); });

Even if the code is written this way, we can also ensure that addcallback before the actual logical execution of the yield.

Afterwards callback

Sometimes, the user of the framework may actually write the code that first yield the addcallback. At this point, I think it is necessary to ensure that the callback function added in Addcallback is immediately triggered. Because the user adds this callback function, it means that he expects to be notified of the callback function when the asynchronous operation has a result, which has nothing to do with whether the asynchronous operation completes when the callback function is added. To do this, we add one more use case:

function psudoAsyncOperation(argument) {
  operation = new Cat.Async.Operation();
  operation.yield("hello world");
  return operation;
}
var operation = asyncOperation(argument);
setTimeout(function() {
  operation.addCallback(function(result) { alert(result); });
}, 1000);

Summary

Here, we have designed a Cat.Async.Operation asynchronous operation object, specifically how to implement the key yield method and Addcallback method will be described in the next article if.

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.