简体   繁体   English

多重依赖承诺链

[英]Multiple dependency promise chain

I'm trying to implement the following use case:我正在尝试实现以下用例:

  1. I've a sequence of sync tasks我有一系列同步任务
  2. Each sync task need to trigger an async task每个同步任务都需要触发一个异步任务
  3. Async tasks cannot run concurrently, so each one wait both for its trigger (2) and the completion of its predecessor.异步任务不能同时运行,因此每个任务都等待其触发器 (2) 及其前任的完成。

在此处输入图片说明

My first idea was to convert every task (sync & async) into a promise then create two promise chains.我的第一个想法是将每个任务(同步和异步)转换为一个承诺,然后创建两个承诺链。 But I'm struggling finding a way to implement the async tasks chain with double dependency.但是我正在努力寻找一种方法来实现具有双重依赖性的异步任务链。

How can I achieve this ?我怎样才能做到这一点?

Actually if your "sync tasks" are really synchronous, you can simply run them synchronously before starting your chain of asynchronous tasks and the condition that every "async task" runs after its respective "sync task" is trivially met, because all async tasks run after all sync tasks.实际上,如果您的“同步任务”真的是同步的,您可以在启动异步任务链之前简单地同步运行它们,并且每个“异步任务”在其各自的“同步任务”之后运行的条件很容易满足,因为所有异步任务都运行毕竟同步任务。

If your "sync tasks" are actually asynchronous themselves, or somehow run really parallel to each other (I don't know about the threading/concurrency model of ExtendScript), you should make each of the tasks (whether "sync" or "async") return a promise.如果您的“同步任务”本身实际上是异步的,或者以某种方式彼此真正并行运行(我不知道 ExtendScript 的线程/并发模型),那么您应该使每个任务(无论是“同步”还是“异步” ") 返回一个承诺。

You can then build your chain network (acyclic graph) of dependencies using Promise.all :然后,您可以使用Promise.all构建依赖项的网络(无环图):

var syncResult1 = syncTask1();
var asyncResult1 = syncResult1.then(asyncTask1);
var syncResult2 = syncResult1.then(syncTask2);
var asyncResult2 = Promise.all([asyncResult1, syncResult2]).then(asyncTask2);
var syncResult3 = syncResult2.then(syncTask3);
var asyncResult3 = Promise.all([asyncResult2, syncResult3]).then(asyncTask3);

(If your "sync tasks" don't need to return promises, just make it syncResult… = syncTask…(syncResult…) ) (如果您的“同步任务”不需要返回承诺,只需将其syncResult… = syncTask…(syncResult…)

You only need one promise chain:你只需要一个承诺链:

syncTask1();
var promise1 = asyncTask1(); // Triggers first async task
syncTask2();
var promise2 = promise1.then( asyncTask2 );
syncTask3();
var promise3 = promise2.then( asyncTask3 );
...

Of course you would want to name your tasks and promises meaningfully.当然,您希望有意义地命名您的任务和承诺。 This will behave identically to this though, since JavaScript is single threaded and all the sync tasks will complete before the promise of asyncTask1 can resolve:这将与此相同,因为 JavaScript 是单线程的,并且所有同步任务都将在 asyncTask1 的承诺可以解决之前完成:

syncTask1();
var promise1 = asyncTask1(); // Triggers first async task
syncTask2();
syncTask3();
...
var promise2 = promise1.then( asyncTask2 );
var promise3 = promise2.then( asyncTask3 );
...

From the diagram it looks like Async2 and 3 depend on more than just fullfillment of the relative Sync function,从图中看起来 Async2 和 3 不仅仅依赖于相关同步功能的实现,

Using your naming conventions this is how I'd chain the promises:使用您的命名约定,这就是我将承诺链接起来的方式:

               Synctask1
               |       |
             Sync2    Async1
             |     \     |
           Sync3    Async2(resolved with promiseAll[Async1, Sync2]
                \     |
                 \    |
                 Async3(resolved with promiseAll[Async2, Sync3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM