Design pattern for many asynchronous tasks in node

I am learning node and writing an API. One of my API calls takes a Tag parameter that will contain comma-separated tags, each of which I want to save to disk (I use MongoDB + Mongoose). Typically, when I save a DB in my API, I pass the callback and continue after saving inside that callback, but here I have a variable number of objects to save to disk, and I donโ€™t know which is the cleanest way to save all these tags to disk, then save the object that references them later. Can anyone suggest a clean asynchronous template? Thanks!

+4
source share
2 answers

async is a good node library for these tasks.

start several asynchronous calls in parallel or sequentially and start one callback after this:

async.parallel([ function(){ ... }, function(){ ... } ], callback); async.series([ function(){ ... }, function(){ ... } ]); 
+8
source

This is a regular code template that I often use when I don't need additional dependencies:

 var tags = ['tag1', 'tag2', 'tag3']; var wait = tags.length; tags.forEach(function (tag) { doAsyncJob(tag, done); }); function done() { if (--wait === 0) allDone(); } 

This code will run doAsyncJob (tag, callback) in parallel for each element of the array and call allDone when each job is completed. If you need to process data continuously (one after another), here is another template:

 (function oneIteration() { var item = tags.shift(); if (item) { doAsyncJob(item, oneIteration); } else { allDone(); } })(); 
+3
source

Source: https://habr.com/ru/post/1442854/


All Articles