Overview
Implement mapAsync(array, asyncFn) that runs asyncFn on each element sequentially (not in parallel) and returns a promise of the mapped array.
Examples
await mapAsync([1,2,3], async x => x * 2); // [2,4,6]
Solution
Reveal solution
async function mapAsync(array, asyncFn) {
const results = [];
for (const item of array) results.push(await asyncFn(item));
return results;
}map-async.js
Map Async
mediumcodingJavaScriptPromises
Overview
Implement mapAsync(array, asyncFn) that runs asyncFn on each element sequentially (not in parallel) and returns a promise of the mapped array.
Examples
await mapAsync([1,2,3], async x => x * 2); // [2,4,6]
Solution
Reveal solution
async function mapAsync(array, asyncFn) {
const results = [];
for (const item of array) results.push(await asyncFn(item));
return results;
}NameTopicDifficulty
103 of 103 problems