Should I be using process.nextTick - node.js

I'm trying to get my head around when to use process.nextTick. Below I'm using async library to control my code flow and was wondering if I should be calling nextTick in the end callback or not.
async.parallel([
function (callback) {
// do something
callback(data);
},
function (callback) {
// do something
callback(data);
}
],
// callback
function (data) {
process.nextTick(function () {
// do something
});
});

Without knowing exactly what your 'do something' is, I don't see any reason that you'd need to use nextTick in this situation. You use nextTick when you have reason to defer execution of a function to the next iteration of the event loop. You might want to read Understanding process.nextTick for more detail.
Also, note that async's parallel task completion callbacks take arguments err, data, so you should be doing:
async.parallel([
function (callback) {
// do something
callback(null, data);
},
function (callback) {
// do something
callback(null, data);
}
],
// callback
function (err, data) {
if (err) throw err;
// do something
});
The major difference being that the final callback is invoked immediately if one of the parallel tasks returns an error (calls callback with a truthy first argument).

My experience is that for async.parallel and other similar functions to work correctly, that you need to make all tasks definitely asynchronous; I love the async lib and it's my #1 choice over promises etc, but there can be some weird behavior if you don't force tasks to be async.
See here:
https://github.com/caolan/async#common-pitfalls-stackoverflow
so it should be:
async.parallel([
function(cb){
process.nextTick(function(){
doSomePotentiallySyncThing(cb);
});
},
function(cb){
process.nextTick(function(){
doSomePotentiallySyncThing(cb);
});
},
function(cb){
definitelyAsync(cb); // if it's **definitely** async, then we don't need process.nextTick
}
], function complete(err, results){
// we are in the final callback
});
I am 99% sure about this.

Related

Node.js Async only doing one callback

I'm just doing my first bit of Node async stuff, I wanted to do two queries to a DB and then print the results one after the other, my code is as follows:
console.log(req.query);
function logAllThings(err,things){
if (err){
console.log(err);
} else {
console.log(things);
};
};
async.parallel([
function(callback) { //This is the first task, and callback is its callback task
Event.find({}, function(err, events) {
logAllThings(err,events);
});
},
function(callback) { //This is the second task, and callback is its callback task
Organisation.find({}, function(err,organisations) {
logAllThings(err,organisations);
}); //Since we don't do anything interesting in db.save()'s callback, we might as well just pass in the task callback
}
], function(err) { //This is the final callback
console.log('Both should now be printed out');
});
The issue I have is that the second function (the one that returns organisations) prints them fine. However the one that is meant to return events simply does not and returns {} despite the fact I know the query works as I've tested it elsewhere.
Any help would be appreciated
Thanks
You have to call the callback function passed to each of your waterfall functions, otherwise it won't know when it's finished. Try this:
async.parallel([
function(callback) { //This is the first task, and callback is its callback task
Event.find({}, function(err, events) {
if (err) return callback(err);
logAllThings(err,events);
callback();
});
},
function(callback) { //This is the second task, and callback is its callback task
Organisation.find({}, function(err,organisations) {
if (err) return callback(err);
logAllThings(err,organisations);
callback();
}); //Since we don't do anything interesting in db.save()'s callback, we might as well just pass in the task callback
}
], function(err) { //This is the final callback
console.log('Both should now be printed out');
});

async.each() final callback is not executed

async.each(spiders, function(){
console.log('hi');
}, function(err) {
if(err) { console.log(err);}
else console.log('ok');
});
After logging 'hi', async didn't execute the callback and logged 'ok' or errors.
What is wrong in my code?
async provides two important parameters to your iterator function: an item and a callback. The first one gives you the actual data item from the array, and the second is a function, to indicate the end of the actual method. The final callback (the one with the log('ok')) gets called, when every iterator call indicated their own callback.
So your code should be something like this:
async.each(spiders, function(item, callback) {
console.log('spider: ' + item);
callback(null);
}, function(err) {
if (err) {
return console.log(err);
}
console.log('ok');
});
The null parameter means there is no error.
Also note, handling error like this, is a better practice.
Quoting from the async.each documentation:
iterator(item, callback) - A function to apply to each item in arr. The iterator is passed a callback(err) which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit null argument.
Conclusion: You forgot to take the callback parameter and therefore did not call it either.

Node using promises in conjunction with async module

I'm using the async module to help with creating an object that depends on other objects that are created asynchronously. The asynchronously created objects have validations that are ran against them that ultimately return a promise. The problem I'm having is that the "final" async callback seems to never be called and I cannot figure out why.
Example code:
function construct(self, opts) {
async.parallel({
venue: function(callback) {
new Venue(opts.venue).then(function(venue) {
callback(null, venue);
}).catch(function(err) {
callback(err);
});
},
artists: function(callback) {
new Artist(opts.artist).then(function(artist) {
callback(null, artist);
}).catch(function(err) {
callback(err);
});
},
function(err, res) {
console.log(res); // THIS IS NEVER CALLED.
}
}
It looks like the issue is that your callback function is inside the object that you're passing to async.parallel instead of as its own argument.
You've got
async.parallel({
venue: func,
artists: func,
callback
})
Instead of
async.parallel({
venue: func,
artists: func,
}, callback
);
BUT, it's really worth questioning what you gain from mixing promises and async like this; they're essentially designed to accomplish the same task; it'd probably be worth picking one or the other. Something like Promise.all or Promise.join is an alternative to async.parallel.
Pure promise version of this would look like: (this is assuming bluebird promises; but other libraries would be similar, if not identical)
Promise.join(new Venue(opts.venue), new Artists(opts.artists))
.then(function(venues, artists) {
//
});
That's a lot cleaner, I think.

async and Q promises in nodejs

I'm using the Q library and async library in nodejs.
Here's an example of my code:
async.each(items, cb, function(item) {
saveItem.then(function(doc) {
cb();
});
}, function() {
});
saveItem is a promise. When I run this, I always get cb is undefined, I guess then() doesn't have access. Any ideas how to work around this?
Your issue doesn't lie with promises, it lies with your usage of async.
async.each(items, handler, finalCallback) applies handler to every item of the items array. The handler function is asynchronous, i.e. it is handed a callback, that it must call when it has finished its work. When all handlers are done, the final callback is called.
Here's how you'd fix your current issue:
var handler = function (item, cb) {
saveItem(item)
.then(
function () { // all is well!
cb();
},
function (err) { // something bad happened!
cb(err);
}
);
}
var finalCallback = function (err, results) {
// ...
}
async.each(items, handler, finalCallback);
However, you don't need to use async for this particular piece of code: promises alone fill this job quite nicely, especially with Q.all():
// Create an array of promises
var promises = items.map(saveItem);
// Wait for all promises to be resolved
Q.all(promises)
.then(
function () { // all is well!
cb();
},
function (err) { // something bad happened!
cb(err);
}
)

async.parallel() - last function not being called

For some reason the 'yyyyyyyyy' string is never printed when I use async.parallel() as per below. Why is this? I thought that the last function would be called once the other two have been called.
var async = require('async');
async.parallel([
function() {
console.log('xxxxxxxxxxx');
},
function() {
console.log('ccccccccccc');
}
], function(err, results){
console.log('yyyyyyyyy');
});
Every function passed in the first parameter to async.parallel should take a callback that it calls when its done so async knows that it has completed:
var async = require('async');
async.parallel([
function(callback) {
console.log('xxxxxxxxxxx');
callback();
},
function(callback) {
console.log('ccccccccccc');
callback();
}
], function(err, results){
console.log('yyyyyyyyy');
});
If an error happens in one of the functions, it should call the callback with
callback(err);
so that async knows an error has occurred and it'll immediately call the last function.

Resources