Reputation: 74
how to make several requests to DB, wait for results from all requests and return results? I try with Promise.all, but all I get - [undefined] in console.
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("DBusers");
var query = {};
var U4 = dbo.collection("users");
const promises = [
U4.find({}).count(function (err, result) { return result; }),
U4.find({}).count(function (err, result) { return result; })
];
Promise.all(promises).then(function (results) {
console.log(results);
}).catch(function (err) {
console.log(err);
});
db.close();
});
Upvotes: 0
Views: 824
Reputation: 127
you use a callback function to Promise.all
array. it's correct way to use Promise.all
:
const promises = [
U4.find({}).count(),
U4.find({}).count()
];
by default that returns a promise and you must be past array of promise to Promise.all
.
Upvotes: 2
Reputation: 74
I think I found an answer, I had to change to:
const promises = [
U4.find({}).count(),
U4.find({}).count()
];
Upvotes: 0