deepseadiving
deepseadiving

Reputation: 763

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!

Upvotes: 4

Views: 1066

Answers (2)

Anatoliy
Anatoliy

Reputation: 30073

This is common code pattern I often use when I don't want 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 item of array, and call allDone when each job completed. If you need to process data continuously (each after another), here's another pattern:

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

Upvotes: 3

lrsjng
lrsjng

Reputation: 2615

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(){ ... }
]);

Upvotes: 8

Related Questions