Skip to content

Instantly share code, notes, and snippets.

@pimterry
Created January 9, 2020 17:56
Show Gist options
  • Save pimterry/bfc06f43c9e658f2de23a152e333404a to your computer and use it in GitHub Desktop.
Save pimterry/bfc06f43c9e658f2de23a152e333404a to your computer and use it in GitHub Desktop.
JS code (untested) to wait for the first success, or throw the last error.
const awaitFirst = (promises) =>
new Promise((resolve, reject) => {
let count = promises.length;
promises.forEach(p =>
p.then(resolve, (e) => {
count = count - 1;
if (count === 0) reject(e);
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment