Put execution of a function on hold using
await pauses execution until an asynchronous
process completes. Execution is unblocked when
defer function is triggered.
defer are used together by passing
to a function that later triggers a callback.
For example, use
done to put
the program on hold until all animation so far
fd 100 write 'before', getxy() await done defer() write 'after', getxy()
In this program, execution is put on hold until
the animation is completed: then when the
callback is triggered, the program proceeds.
Callbacks that receive an argument can be
used to assign a value with
read normally calls a callback
with the value entered by the user:
read 'your name?', (x) -> write 'hello', x
await, the same program can be written as:
await read 'your name?', defer x write 'hello', x
await is particularly useful inside looping