How Do I Nest A Promise Inside Another Promise Function In Node.js?
I have a file file which is surrounded by Promise function . I have a database operation inside this function which requires another promise too . Please check the code below var p
Solution 1:
Surrounding the whole thing by one new Promise
call doesn't help anything. Inside it, you'd still have callback hell. And no, throwing Promise.resolve()
at a function that doesn't return anything doesn't help either.
You will need to promisify the asynchronous primitives, i.e. the smallest parts that are asynchronous. In your case, that's distance.matrix
and mongo's connect
+insert
:
function getMatrix(m, o, d) {
return new Promise(function(resolve, reject) {
m.matrix(o, d, function(err, distances) {
if (err) reject(err);
else resolve(distances);
});
});
}
function save(url, store, k) {
// cramming connect+insert in here is not optimal but let's not get into unnecessary detail
return new Promise(function(resolve, reject) {
MongoClient.connect(url, function(err, db) {
if (err)
reject(err);
else
db.collection(k).insert(store, function(err, results) {
if (err) reject(err);
else resolve(results);
db.close();
});
});
});
}
Now that we have those, we can actually use them and combine our promises into what you actually are looking for:
module.exports = Promise.all(dep.map(function(name) {
distance.departure_time(name);
returngetMatrix(distance, origins, destinations).then(function(distances) {
if (!distances) thrownewError('no distances');
var promises = [];
if (distances.status == 'OK') {
for (var i=0; i < origins.length; i++) {
for (var j = 0; j < destinations.length; j++) {
var origin = distances.origin_addresses[i];
var destination = distances.destination_addresses[j];
if (distances.rows[0].elements[j].status == 'OK') {
var duration = distances.rows[i].elements[j].duration_in_traffic.value;
var myobj = {
destination: destination,
departure_time: name,
duration: duration
};
var str = destination.replace(/[,\s]+/g, '');
promises.push(save(url, myobj, str));
// ^^^^^^^^^^^^^^^^^^^^^
}
}
}
}
returnPromise.all(promises); // now wait for all save results
});
}));
Post a Comment for "How Do I Nest A Promise Inside Another Promise Function In Node.js?"