I'm using a library called request-promise
to make my calls to an API multiple times over a look with promises like so.
const rp = require('request-promise');
static buildProfileImageObject(personObj) {
var promiseArr = [];
var parsedPersonResponseObj = [];
personObj.searchResult.hits.map(person => {
let options = {
method: 'GET',
uri: `api.com/class/${person.ID}`,
json: true
}
promiseArr.push(rp(options));
});
var resultsObj = null
Promise.all(promiseArr)
.then((results) => {
resultsObj = results
console.log(results)
}).catch(err => {
Utils.log("API Call Error", err)
return Utils.errorResponse();
});
console.log("THIS IS AFTER");
console.log(resultsObj);
return resultsObj;
}
This calls the API perfectly and shows all the results when console.log(results)
is hit.
My issue is that when this function is called I get a return of value of null
because it's not waiting for Promise.all
to finish. The response appears in my console after some time after the return is showing null
How do I wait for the Promises to finish and then return those results instead?