Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern for many asynchronous tasks in node

I'm learning node and writing an API. One of my API calls takes a parameter called Tags, which will contain comma-delimited tags, each of which I want to save to disk (I'm using MongoDB + Mongoose). Typically when I save to DB in my API I pass a callback and carry on after the save inside of that callback, but here I have a variable number of objects to save to disk, and I don't know the cleanest way to save all of these tags to disk, then save the object which references them afterward. Can anyone suggest a clean async pattern to use? Thanks!

like image 259
deepseadiving Avatar asked Dec 20 '22 14:12

deepseadiving


1 Answers

async is a good node library for these tasks..

run multiple async calls in parallel or in series and trigger one single callback after that:

async.parallel([
    function(){ ... },
    function(){ ... }
], callback);

async.series([
    function(){ ... },
    function(){ ... }
]);
like image 196
lrsjng Avatar answered Dec 24 '22 00:12

lrsjng