Tuesday 23 May 2017

Nested async calles usin

In NodeJs there are a lot of techniques/libraries which helps us create asynchronous work, e.g. Node async library, Promises, using NodeJs standard calls (setImmediate/setTimeout/nextTick).

In relation to server side applications, Sometimes I run into code like this:

function doWork() {
    async.parallel([work1func, work2func],
    (err, result) => { doSomthingWithTheResult(); }
}

function work1func(cb) {
    doSomeStuff1();
    async.series([moreWork1, moreWork2…], cb);
}

function work2func(cb) {
    doSomeStuff2();
    async.series([moreWork3, moreWork4…], cb);
}

As you can see, within work1Func, work2Func there are also async inner-calls to other functions using async.series as well (could have been async.parallel or any other async mechanism).

Q: Is there any benefit having nested async calls? I am asking because if the top most call is async why making nested calls async as well if we are already running asynchronously.

Can someone please explain (maybe give an example), and also give some rules of thumbs here ? when should I nest async call within async call and when just a single async is suffice.



via Mercury

No comments:

Post a Comment