<await> multiple promises in Marko

210 Views Asked by At

In Marko, how can one <await> multiple promises in parallel?

For example, I am looking for something like this:

<await(promise1, promise2)>
  <@then|result1, result2|>
    promise1 and promise2 are resolved in parallel
    ${result1} renders whenever it is ready.
    ${result2} renders whenever it is ready.
  </@then>
</await>

2

There are 2 best solutions below

0
On

You can use Promise.all to accomplish this.

<await(Promise.all([promise1, promise2]))>
  <@then|[result1, result2]|>
    promise1 and promise2 are resolved in parallel
    ${result1} renders whenever it is ready.
    ${result2} renders whenever it is ready.
  </@then>
</await>

This would run the @then block once all of the promises have finished.

0
On

I would add in addition to @Piercey4's response that if you have multiple tags, they run in parallel, so if you don't need the results together, you can wrap them separately:

promise1 and promise2 are resolved in parallel

<await(promise1)>
  <@then|result1|>
    ${result1} renders whenever it is ready.
  </@then>
</await>

<await(promise2)>
  <@then|result2|>
    ${result2} renders whenever it is ready.
  </@then>
</await>

With the default (in-order) flushing, promise2 will be evaluated and rendered in parallel with promise1, but if promise1 is not yet finished the content will be buffered to preserve HTML source order. However, this would allow the content for promise1 to arrive earlier if it finished first.