async-find

Evaluate an array of asynchronous functions, one by one, until one resolves.

Usage no npm install needed!

<script type="module">
  import asyncFind from 'https://cdn.skypack.dev/async-find';
</script>

README

async-find

Based upon the behaviour of Array.find, an asynchronous function which iterates an array of asynchronous functions (or Promises), one by one sequentially, evaluating each one in turn until one resolves.

As soon as one resolves, the resolved response is returned and the process halts.

If all functions reject, the function itself rejects with a specific ExhaustionError. This can be identified by checking the code property on the error object equals ERR_FUNCTIONS_EXHAUSTED.

ExhaustionError objects have an internalErrors property, an Array of each rejected error that occured whilst iterating the Promises.

Example

const asyncFind = require('async-find');

async function rejectingAsyncFunction(i) {
  console.log(i);
  throw new Error();
}

async function resolvingAsyncFunction() {
  return 'Resolved!';
}

asyncFind([
  () => rejectingAsyncFunction(1),
  () => rejectingAsyncFunction(2),
  resolvingAsyncFunction,
  () => rejectingAsyncFunction(3),
])
  .then(console.log);

Will return:

1
2
Resolved!

The final function is never called, because the previous function resolved.

Author

License

MIT