I have a function that calls an API, and the API accepts a callback:
const callApi = async (param1, param2) => {
api.doSomething(param1, param2, (result) => {
// I want to return the result as the result of callApi
}
}
And I have a list/array of objects on which I want to call the Api function, treat the results one by one, and then sort them and pick the one that fits my criteria.
I do NOT want to shove all the logic in the callback. I know I could, but it would result in ugly looking code that will be hard to read 1 year from now.
Something like:
let results = myArrayOfObjects.map(function(myObject){
callApi(myObject.field1, myObject.field2)
.then(
// here I'd like to get the result of api.doSomething
// do more stuff here with that result
)
// return an object here, or a promise
})
// do something with the results after all objects in myArrayOfObjects have been processed
// for example, sort()
If the results are promises, I am thinking of using Promise.all to wait for all of them to complete. Otherwise, it would be even easier to work with the results.
Thank you.