I have a function, which contains code along the lines of...
db.query(query).then(results => {
_.each(results, result => {
db.query(anotherQuery).then(
/* modify result based off anotherQuery results */
});
});
resolve(results);
});
What is happening, of course, is that resolve(results))
is being hit whilst the second request to the DB is being run, which means that the promise is not resolved to add the additional data before being returned.
Is there a pattern I can use to avoid this? I am using Sequelize, which is backed by Bluebird for Promises. It's a pattern I've come across a few times, and I've already looked at using wait.for but it seems to be outdated and unsupporting of Promises.