0

在 Marko 中,一个多个 promise 如何<await>并行?

例如,我正在寻找这样的东西:

<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>

4

2 回答 2

2

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.

于 2021-06-11T20:43:34.180 回答
1

除了@Piercey4 的回复之外,我还要补充一点,如果您有多个标签,它们会并行运行,因此如果您不需要将结果放在一起,您可以将它们分开包装:

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>

使用默认(按顺序)刷新,promise2将与 并行评估和呈现promise1,但如果promise1尚未完成,内容将被缓冲以保留 HTML 源顺序。但是,如果内容先完成,这将允许内容promise1更早到达。

于 2021-06-14T20:08:45.703 回答