parallel expressions allows you to do things in parallel, and then do things with their results.
A parallel expression is built up from multiple parts:
- expressions or statements that return a
Promiseor aResult - an optional
thenbranch catchbranches- an optional
finallybranch
An example:
parallel {
articles =
loadImages()
users =
loadUsers()
} then {
next {
articles = articles,
users = users,
}
} catch {
/* errors are catched here. */
} finally {
/* Clean things up here. */
}
Keep in mind that you need to handle all possible errors that can be returned from a statement, although the compiler has your back here and will show an error if you forget one.
The return value of a parallel expression is always a Promise(error, result) :
- If the value of the
thenbranch is aPromisethen it will be returned as is - If the value of the
thenbranch is not a Promise then it will return a promise which never failsPromise(Never, result)where theresultis the value of thethenbranch - If there is no
thenbranch then the return value isPromise(Never, Void)
A few notable things to keep in mind:
- All of the catches must match the return value of the last statement
- Results and unwrapped into variables
- Promises are
await-ed and unwrapped into variables