January 2020
Intermediate to advanced
470 pages
11h 13m
English
Can we use the other functions? Writing mapAsync(), a version of map that can work with an async mapping function, is simple because you can take advantage of Promise.all() to create a promise out of an array of promises:
const mapAsync = (arr, fn) => Promise.all(arr.map(fn));(async () => { console.log("START MAP"); const mapped = await mapAsync([1, 2, 3, 4], async n => { const x = await fakeAPI(n * 1000, n); return x; }); useResult(mapped); console.log("END MAP");})();/* START MAP2019-10-13T20:06:21.149Z [ 1, 2, 3, 4 ]END MAP*/
The structure of the solution is similar to the forEachAsync() code. As before, we must remember to await the result of mapAsync() before continuing the process. Other than that, the logic is straightforward, ...
Read now
Unlock full access