If you need asynchronous tasks to be synchronized, you should proceed with
the next "stage" in the previous stage's `then` block:
function getConnection (server, databaseName) {
// ...
}
function executeServerQueries (connection, queries) {
var queryPromises = queries.map(function (query) {
return connection.execute(query);
});
// the 'catch' below insures this function always returns a resolved
promise
return Promise.all(queryPromises)
.catch(function (err) {
console.log(err);
return 'No results!';
});
}
function executeQueries (configArr) {
if (!configArr.length) return Promise.resolve();
var config = configArr.unshift();
return getConnection(config.sqlServer, config.databaseName)
.then(function (connection) {
return Promise.all([
executeServerQueries(connection, config.queries),
connection
]);
})
.then(function (outcome) {
var connection = outcome.pop(),
results = outcome.pop();
connection.close();
// do something with results?
return executeQueries(configArr);
});
}
Now, `executeQueries` just takes an array of configurations and returns a
`Promise` after all have completed. I forgot if Node.js has native
`Promise.all` support, but you can use something like bluebird if not. It
can also be cleaned up a bit by using some other bluebird methods and es6
syntax (but I forgo'd these since I didn't see you use them).
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/07ff6615-a310-44ed-a237-d96ef24375f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.