I have this code where I only get result from 1 Promise, I tried each one individually by commenting one of them, they actually resolve with no error, but when they are together, i only get the result of the first one. But not from Promise.all
, only the console.log
inside the Promise gets called
var mongodb = require('mongodb');
var Promise = require('bluebird');
var $ = mongodb.MongoClient.connect('mongodb://nobody:nobody@localhost/dbauth');
$.then(
(db) => {
var p1 = new Promise(
(resolve, reject) => {
var dbx = db.db('db1');
dbx.authenticate('user1', 'user1').then((x) => {
console.log(x); // true
var o = dbx.stats();
o.then(
(result) => {
console.log(result);
resolve(result);
}
).catch(
(err) => {
reject(err);
}
);
});
});
var p2 = new Promise(
(resolve, reject) => {
var dbx = db.db('db2');
dbx.authenticate('user2', 'user2').then((x) => {
console.log(x);
var o = dbx.stats();
o.then(
(result) => {
console.log(result);
resolve(result);
}
).catch(
(err) => {
reject(err);
}
);
});
});
return Promise.all([p1,p2]).then(
(res) => {
res.forEach(console.log);
}
).catch(console.log);
}).catch(console.log);
what prints is this
true // from console.log(x); of p1
true // from console.log(x); of p2
{ db: 'db1',
collections: 2,
views: 0,
objects: 0,
avgObjSize: 0,
dataSize: 0,
storageSize: 8192,
numExtents: 0,
indexes: 2,
indexSize: 8192,
ok: 1 } // from console.log(res) of p1
but the same code will work fine if I remove this part from the code from both promises and call resolve(x)
instead
o.then(
(result) => {
console.log(result);
resolve(result);
}
).catch(
(err) => {
reject(err);
}
);
Promise.all([p1]);
why actually the promise is being executed before it comes to Promise.all
?
via user7887107
No comments:
Post a Comment