Windjs's $await essay

Source: Internet
Author: User

The first knowledge of Windjs, little understanding, and no documents and APIs, only their own to find the need for the functional modules, every knowing, then an essay.

$await is the core API of Windjs. Specific check author Lao Zhao blog: on Jscex's $await semantic and asynchronous task model

The semantics of $await are actually just "waiting for the end of the task", while:

    • If the task is not running, start the task.
    • If the task is completed, the result is returned immediately (or throws an exception).

Suppose there are three tasks a, B, C, and the execution time is 3000ms,5000ms,0ms respectively.

Scenario One: Parallel

Assume that three tasks are parallel, non-interference, respectively executed, here with the general JS can, WINDJS implementation as follows: (overqualified)

1 var A = eval (wind.compile ("Async", function () {2 console.log ("Start A");3 $await (Wind.Async.sleep);4 console.log ("Finish A");5 }));6 7 var B = eval (wind.compile ("Async", function () {8 console.log ("Start B");9 $await (Wind.Async.sleep);Ten console.log ("Finish B"); One })); A  - var C = eval (wind.compile ("Async", function () { - console.log ("Start C"); the console.log ("Finish C"); - })); -  - A (). Start (); + B (). Start (); -C (). Start ();
View Code

Scenario two: serial

Assuming that three tasks are serial, a executes execution b,b executes to execute c. General JS can be used settimeout to do callbacks. This is a bit of a mess, our center is the function itself, do not want to mess up the order of the problem. The WINDJS is implemented as follows:

1 var A = eval (wind.compile ("Async", function () {2 console.log ("Start A");3 $await (Wind.Async.sleep);4 console.log ("Finish A");5 }));6 7 var B = eval (wind.compile ("Async", function () {8 console.log ("Start B");9 $await (Wind.Async.sleep);Ten console.log ("Finish B"); One })); A  - var C = eval (wind.compile ("Async", function () { - console.log ("Start C"); the console.log ("Finish C"); - })); -  - var run = eval (wind.compile (' async ', function () { + $await (A ()); - $await (B ()); + $await (C ()); A })); at  -Run (). Start ();
View Code

Here $await is the equivalent of suspending the thread until the function of the parameter runs out of line friend continues execution.

Scenario Three: Parallel + serial (dependent)

    Assuming that tasks A and B can be done at the same time, and C depends on a and B, they need to be executed when they are complete. General JS can write C in a and B callbacks, to ensure that A and B are executed in the case of the execution of the callback function C. Can only highlight one annoying word ... Windjs Way:

1 var A = eval (wind.compile ("Async", function () {2 console.log ("Start A");3 $await (Wind.Async.sleep);4 console.log ("Finish A");5 }));6 7 var B = eval (wind.compile ("Async", function () {8 console.log ("Start B");9 $await (Wind.Async.sleep);Ten console.log ("Finish B"); One })); A  - var C = eval (wind.compile ("Async", function () { - console.log ("Start C"); the console.log ("Finish C"); - })); -  - var run = eval (wind.compile (' async ', function () { + $await (Task.whenall (A (), B ())); - $await (C ()); + })); A  atRun (). Start ();
View Code

Temporarily Task.whenall () API error ...

  

  

Windjs's $await essay

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.