Stop promises execution once the first promise resolves

Viewed 2013

I'm using ES6 promises, the idea of this function is to loop over an array of links, and for each link look for an image and stop once an image found.

In this case of the function that I wrote, the fastest promise resolves and others keeps executing, so what I want is to stop the execution of the remaining promises as soon as the first one resolves.

scrapImage(links) {
  let promises = links.map((l) => getImageUrlAsync(l));
  return Promise.race(promises);
}
2 Answers
Related