Skip to content

Instantly share code, notes, and snippets.

@justinfagnani
Created October 17, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfagnani/5a81c026cb1d10efe6fe028d7a232c5d to your computer and use it in GitHub Desktop.
Save justinfagnani/5a81c026cb1d10efe6fe028d7a232c5d to your computer and use it in GitHub Desktop.
findIndexAsync.js
async function findIndexAsync(promises, predicate) {
let i = 0;
for await (const p of asyncIterate(promises)) {
if (predicate(p)) {
return i;
}
i++;
}
}
async function* asyncIterate(promises) {
for (const p of promises) {
yield p;
}
}
const promises = [Promise.resolve(false), Promise.resolve(true)];
(async () => {
console.log(await findIndexAsync(promises, (v) => v));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment