简体   繁体   中英

How to return a promise when an array of functions is passed, to another function as an arguments?

A question can be found here :

Implement the getInParallel function that should be used to invoke multiple API calls in parallel. The function accepts an array of functions that return a Promise . The function should return a Promise which 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!' ] [ '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?

Your answer is pretty straight-forward, and maybe the simplest one.
I will rewrite getInParallel as below (usually we need type checking, omited here)

const parallel = funcs => Promise.all(funcs.map(fn => fn()));

Your solution is pretty close yet needs some modifications. Please check the code below:

 function getInParallel(apiCalls) { // Write your code here let functions = apiCalls.map(apiCall => apiCall()); return Promise.all(functions).then(response => response); } 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)); }

Here is a clean way to go about it

 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 resFunt = apiCalls.map((funct) => { return funct() }) return Promise.all(resFunt).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;

Here is the short way.

function getInParallel(apiCalls) {
  return Promise.all(apiCalls.map(f=>f()));
}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM