FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Map Async Limit

25mins

Implement a function mapAsyncLimit(items, fn, size) that maps over an array with an asynchronous function but limits the number of concurrently running async tasks.

The function should:

  • Accept an array of items.
  • Accept an async mapping function fn that returns a Promise.
  • Accept an optional concurrency limit size. If not provided, all tasks can run concurrently.
  • Return a Promise that resolves to an array of mapped results in the original order.

Example

async function fetchUpperCase(q) { return q.toUpperCase(); } const results = await mapAsyncLimit(['foo', 'bar', 'qux'], fetchUpperCase, 2); console.log(results); // ['FOO', 'BAR', 'QUX']

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests