I am trying to make two HTTP requests to retrieve data, each of which has a callback function. Only after both callback functions have completed do I want to run the final bit of code. Maybe it's my unfamiliarity with promises, but I can't seem to get this to work. I've only gotten it to run the final thenable code either immediately or never.
var p1 = getStatus(account1, currency, processStatus)
var p2 = getStatus(account2, currency, processStatus)
Promise.all([p1, p2]).then(function() {
// evaluate complete status
})
getStatus
is a coffeescript function that makes an HTTP request, and once the data is retrieved, it calls the provided callback function, which is the third parameter.
getStatus: (acctId, curr, callback) =>
options = {url: url, account: acctId, currency: curr}
new Promise => request options, (err, resp, body) =>
if !err
return Promise.resolve callback(null, acctId, curr, JSON.parse(body))
processStatus
is a JavaScript function that crunches through the retrieved data.
module.exports.processStatus = function(err, acctId, curr, status) {
status.forEach(function(s) {
// ....
})
return Promise.resolve(true)
})
How do I change this to make the evaluate complete status
code execute only after both processStatus
callbacks have completed?