node.js - Promises in a for loop, do something when done -


basically, i'm trying figure out way bunch of queries in loop , once they've completed.

i got work, because used great library wraps of mongodb promises.

var mongo = require('mongod')  var db = mongo('mongodb://localhost/builder', ['block'']) var block_ids = ['538d097bbb12479d0e9f70ab', '538ddc7d06c1f0fe296178b1'];  var prom = null; var blocks = []; (i in block_ids) {     var block_id = block_ids[i];     prom = db.block.findone({_id:db.objectid(block_id)})     .then(function(results) {         blocks.push(results);     }) } prom.done(function() {     console.dir(blocks)     console.log("done! " + blocks.length) }) 

my question this. how on earth can without promises?? seems hard!

stupid loops

var mongo = require('mongod')  var db = mongo('mongodb://localhost/builder', ['block'']) var block_ids = ['538d097bbb12479d0e9f70ab', '538ddc7d06c1f0fe296178b1'];   q(block_ids.map(function(block_id) {     return db.block.findone({_id:db.objectid(block_id)}); })).all().done(function(blocks) {     console.dir(blocks)     console.log("done! " + blocks.length) }); 

i got work, because used great library wraps of mongodb promises.

in bluebird have done promisifyall(require("mongodb")) instead of waiting make module.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -