Code Lab

Modo entrevista. Temporizador de 5 min, sin pistas y sin autocompletado

Parallel requests

FácilAsync code5 min

The function load(id) loads a user and returns a promise with the user data. Write a function loadAll(ids, load) that starts loading all ids at once and returns a promise with an array of the results.

The results keep the order of ids, even when the answers arrive in another order.

Ejemplos

await loadAll([1, 2], (id) => Promise.resolve('user ' + id))

Devuelve [ 'user 1', 'user 2' ]

Each id becomes the result of its own load.
await loadAll([], (id) => Promise.resolve(id))

Devuelve []

An empty list gives an empty array.

Fuente

Cargando el editor…
  1. Order of ids, though the answers arrive out of order
  2. All three loads run at the same time

Las pruebas ocultas se ejecutan al enviar