p-try-each
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

p-try-each Build Status

Runs promise-returning functions in series but stops whenever any of the functions were successful. If all functions reject, the promise will be rejected with the error of the final task

Similar to async/tryEach.

Install

npm install --save p-try-each

Usage

const pTryEach = require('p-try-each');
 
const tasks = [
  () => Promise.resolve('foo'),
  () => Promise.resolve('bar'),
  () => Promise.resolve('baz')
]
 
(async () => {
  const res = await pTryEach(tasks);
  console.log(res);
  // Logs:
  // 'foo'
})();

API

pTryEach(tasks)

Returns a Promise that is fulfilled with the value of the first fulfilled promise returned from calling the functions in tasks. If none is fulfilled, the promise will be rejected with the error of the final task.

tasks

Type: Iterable<Function>

Functions must return a value. In case of a returned promise, it will be awaited before starting with the next task.

License

MIT

Package Sidebar

Install

npm i p-try-each

Weekly Downloads

5,092

Version

1.0.1

License

MIT

Unpacked Size

4.85 kB

Total Files

5

Last publish

Collaborators

  • pedromiguelss