# Proposal: `await.all {...}` for parallelism

manuelbarzi manuelbarzi at gmail.com
Tue Nov 26 11:32:32 UTC 2019

```by the way, i think it can also be improved without the need of `async {}`
blocks, just marking together the parallel awaits (`await||`) and write the
code like in series, but grouping the parallel awaits when transpiling:

// NOTE async {} = asynchronous scope (it groups parallel awaits => `await||`)
// NOTE p? = function call that returns a promise (? = just and index)

const x0 = await p0()

const x11 = f11() // sync code in-the-middle

const x1 = await || p1(x0)
const x3 = await || p3(x11)
const x2 = await p2(x1)
const x10 = await p10(x2, x3)

const x4 = await || p4(x1, x2)
const x5 = await || p5(x2, x3)
const x6 = await p6(x4, x5, x10)

const x7 = await || p7(x4, x6)
const x9 = await || p9(x5, x6)
const x8 = await p8(x6, x7)

await p11(x8, x9)

// it would resolve a tree of parallel and series like following with

p0
.then(x0 => {
const x11 = f11()

return Promise.all([p1(x0), p3(x11)])
.then((x1, x3) =>
p2(x1)
.then(x2 =>
p10(x2, x3)
.then(x10 =>
Promise.all([p4(x1, x2), p5(x2, x3)])
.then((x4, x5) =>
p6(x4, x5, x10)
.then(x6 => Promise.all([p7(x4,
x6), p9(x5, x6)])
.then((x7, x9) => p8(x6, x7)
.then(x8 => p11(x8, x9))
)
)
)
)
)
)
})

On Tue, Nov 26, 2019 at 12:21 PM manuelbarzi <manuelbarzi at gmail.com> wrote:

> hi Tom
>
> So the parallel awaits would execute before the series awaits?
>>
>
> not exactly. it would group the parallel awaits at the position the first
> parallel await|| is sentenced.
>
> following the demo i sent:
>
>
> // NOTE async {} = asynchronous scope (it groups parallel awaits => `await||`)
> // NOTE p? = function call that returns a promise (? = just and index)
>
> async {
>     const x1 = await || p1()
>     const x2 = await p2(x1)
>     const x3 = await || p3()
>     const x10 = await p10(x2, x3)
>
>     let x4, x5, x6
>
>     async {
>         x4 = await || p4(x1, x2)
>         x5 = await || p5(x2, x3)
>         x6 = await p6(x4, x5, x10)
>     }
>
>     let x7, x8, x9
>
>     async {
>         x7 = await || p7(x4, x6)
>         x8 = await p8(x6, x7)
>         x9 = await || p9(x5, x6)
>     }
>
>     await p11(x8, x9)
> }
>
> in the outer block, the first sentence is already parallel, ok, then all
> parallel awaits in this block will group at first position, siblings of
> this first one (that is p1 and p3).
>
> in the inner blocks too (just a casualty i created the demos this way, but
> could be different and still work). in the first inner block, p4 and p5,
> and in the second inner block, p7 and p9.
>
> "transpiling" it to promises, it would resolve a complex scenario like
> following (the demo could be improved, of course):
>
>
> // it would resolve a tree of parallel and series like following with traditional promises
>
> Promise.all([p1(), p3()])
>     .then((x1, x3) =>
>         p2(x1)
>             .then(x2 =>
>                 p10(x2, x3)
>                     .then(x10 => {
>                         let x4, x5, x6
>
>                         return Promise.all([p4(x1, x2), p5(x2, x3)])
>                             .then(results => [x4, x5] = results)
>                             .then(() => p6(x4, x5, x10))
>                             .then(_x6 => x6 = _x6)
>                             .then(() => {
>                                 let x7, x8, x9
>
>                                 return Promise.all([p7(x4, x6), p9(x5, x6
> )])
>                                     .then(results => [x7, x9] = results)
>                                     .then(() => p8(x6, x7))
>                                     .then(_x8 => x8 = _x8)
>                                     .then(() => p11(x8, x9))
>                             })
>                     })
>             )
>     )
>
> What if there are series awaits before the first parallel await?
>>
>
> no problem, if there was one before, then in it would be chained first in
> the outer promise, and following it would be the promise.all for the next
> parallel group.
>
>
>> What if the parallel awaits depend on the results of those series awaits?