A question can be found here:
Implement the
getInParallelfunction that should be used to invoke multiple API calls in parallel. The function accepts an array of functions that return aPromise. The function should return aPromisewhich should resolve to an array of results from the apiCalls argument.For example, calling the following code should print
[ 'First API call!', 'Second API call!' ]:let promise = getInParallel([() => Promise.resolve("First API call!"), () => Promise.resolve("Second API call!")]); if(promise) { promise.then((result) => console.log(result)).catch((err) => console.log(err)); }
This is my solution:
function getInParallel(apiCalls) {
// extract function one by one from array
// then return each function's Promise to 'rFunt' as Promise.all only accepts array, iterable of promises as an input
let rFunt = apiCalls.map((funct) => {
return funct()
})
return Promise.all(rFunt).then((res) => {
return res
})
}
let promise = getInParallel([() => Promise.resolve("First API call!"),
() => Promise.resolve("Second API call!")]);
if (promise) {
promise.then((result) => console.log(result)).catch((err) => console.log(err));
}
module.exports.getInParallel = getInParallel;
Are there any other ways/concept to solve this?