attempts
Version:
Try func with different args.
40 lines (26 loc) • 1.06 kB
Markdown
Try func with different args.
- `attempts.sync(values, fn)` *⇒ result / `undefined`*
Invoke `fn` with `values[n]` one by one, until get a truthy return value.
- `attempts.async(values, fn)` *⇒ `Promise` (resolved with result / rejected with `undefined`)*
Invoke `fn` with `values[n]` one by one, until get a truthy return value or
resolved Promise.
NOTE: Exceptions within `fn` will be catched & treated as a falsy return, handy for
some Node.js API (`fs.accessSync`, `fs.statSync`, etc.) throw errors regularly.
## Example
Get availiable config:
```javascript
const attempts = require('attempts')
const readConfig = filename => require(filename)
const possibleConfigs = [
'./config.json',
'./config.default.json'
]
const config = attempts.sync(possibleConfigs, readConfig)
```
MIT © [Amio][author]
[]:https://img.shields.io/npm/v/attempts.svg?style=flat-square
[]: https://www.npmjs.com/package/attempts
[]: https://github.com/amio