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)
applieshandler
to every item of theitems
array. Thehandler
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:
However, you don't need to use
async
for this particular piece of code: promises alone fill this job quite nicely, especially withQ.all()
: