Async functions run asynchronously, consuming Promises and returning one too.
Just use $\
instead of *\
. Generators are never async.
Await a promise using $
. This allows you to write asynchronous code without higher-order functions.
import global fetch $fetch-text = $\url response = $ fetch url $ response.text() $fetch-text "./fetch-me.txt"
These are actually compiled to generators with a simple wrapper around them.
That's the awesome power of generators!
$for
$for
is just like a for that runs all iterations together concurrently, rather than one-by-one.
The value of the $for
is a Promise for an Array of the results of each iteration.
It essentially compiles to Promise.all (@map @coll for-body)
.
So, you must always provide a collection to iterate over, and break
is forbidden.
import global fetch $for ["./fetch-me.txt" "./fetch-me-too.txt"] $ ($ fetch_).text()
$for
can appear as a statement inside $\
.
In statement context, it is automatically awaited equivalent to $ $for ...
.
import global fetch $fetch-map = $\urls with empty Map as map $for url of urls map[url] := $ ($ fetch url).text() $fetch-map ["./fetch-me.txt" "./fetch-me-too.txt"]