asyncMap

Written on August 9, 2019

문제

Implement the function asyncMap:

asyncMap has two parameters, an array of asynchronous functions (tasks) and a callback. Each of the tasks takes a separate callback and invokes that callback when complete.

The callback passed to asyncMap is then performed on the results of the callbacks of the tasks.

The order of these results should be the same as the order of the tasks. It is important to note that this is not the order in which the tasks return, but the order in which they are passed to asyncMap. Once all the callbacks of the tasks are returned, asyncMap should invoke the callback on the results array.

풀이

var asyncMap = function(tasks, callback) {
  let promises = [];
  for (let i = 0; i < tasks.length; i++) {
    let promise = new Promise(tasks[i]);
    promises.push(promise);
  }
  Promise.all(promises).then(value => callback(value));
};

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself