Bluebird Promisfy.each, with for-loops and if-statements?
As Benjamin said, instead of using for
loop, use Promise.each
(or .map
)
Look on the Bluebird API docs here and search "example of static map:". With map
is clearer to understand than docs for each
var Promise = require('bluebird')// promisify the entire mongoose Modelvar Message = Promise.promisifyAll(Models.Message)Promise.each(repliesIDsArray, function(replyID){ return Message.findOneAsync({'_id': req.params.message_id}) .then(function(doc){ // do stuff with 'doc' here. })})
From the docs, .each
(or .map
) takes "an array, or a promise of an array, which contains promises (or a mix of promises and values)
", so that means you can use it with array of 100% pure values to kickoff promise chain
Hope it helps!