PromiseContext
PromiseContext gives a useful asynchronous executing context.
- easy chaining of executors
- optional completion routine
- deferred subroutine call
- subroutine can be looped
- break/continue a loop
- labelled break/continue
Basic
Let A be an example executor below.
{ console // 100ms}
Let B and C are executors similar to A.
In order to execute these executors in a row, do the following:
const PromiseContext = var ctx = ctxctxctxctx
A is called asynchronously after ctx.end(). B is called after A is resolved. C is called after B is resolved.
As a result of this example, following sequence will be printed on console:
in A
out A
in B
out B
in C
out C
Following code is equivalent to the above code.
Catching an error or a rejection
Same manner as Promise.
ctxctx
Completion
Completion routines can be set as follows:
ctx
One of completion routines will be called after all the executors resolved.
Subroutine
var ctx = ctxctxctxctx
This example obtains the same result as ctx.chain(A).chain(B).chain(C).chain(D) although the function 'sub' is executed asynchronously after A is resolved.
Loop
var i=0ctx
This example obtains the same result as ctx.chain(A).chain(B).chain(A).chain(B).chain(A).chain(B) although each iteration is executed asynchronously after the previous iteration is resolved.
Break a labelled loop
ctx
Continue a loop
ctx