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