How to implement multiple tasks waiting for one task?

Well, in Node.js or in Clojure...

I ran into a problem — all of the requests are relying on a task on the server. So they have to wait until the task is finished.

I got one solution from my friend — use promise to wrap the task and let others use yield to wait for that task. And my code is using Koa, it looks strange to create a Promise inside the generator functions.

Do you have any good ideas doing this, in JavaScript(Node 6.x), or in Clojure?

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more

Marco Alka's photo

As for Node.JS (I do not know Clojure):

Since you did not provide any source, I guess the best solution is to use promises. I do not know why you would use generator functions, though. Please elaborate.

Alternatively you could use a callback, but Promises are more flexible imo.

Denny Trebbin's photo

Promise is the right choice for now.

Promise.all([/* array of promises */])
  .then(/* runs when all promises from the array are resolved */)
  .catch(/* runs when any of the promises rejects */)

In the near future you have more options.