https://github.com/inker/tryfunc
https://github.com/inker/tryfunc
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/inker/tryfunc
- Owner: inker
- Created: 2017-10-28T17:44:35.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-17T19:25:24.000Z (almost 2 years ago)
- Last Synced: 2025-03-29T06:33:45.342Z (about 1 year ago)
- Language: TypeScript
- Size: 487 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tryfunc
[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]
`tryCall`: try invoking the function until it runs without throwing. Returns the value returned by the function.
`tryUntil`: try invoking the function until the returned value satisfies the provided validating function. Returns the value returned by the function.
Arguments:
* `func`: the function to invoke with the `iteration` as a parameter
* (only in `tryUntil`) `validate`: the validating function. Stops executing once the validator returns `true`.
* `Options` object:
* `numAttempts`: the number of iterations
* `interval`: the interval between each attempt
* `onAttempt` (optional): the function to invoke after each attempt
## Installation
```
npm install --save tryfunc
```
## Usage
Basic usage:
```javascript
import { tryCall, tryUntil } from 'tryfunc'
function throwingFunction() {
// ...
}
async function foo() {
// try calling function
try {
const val = await tryCall(throwingFunc, {
interval: 100,
numAttempts: 10,
onAttempt: (err, i, success) => {
if (err) {
console.error(err)
}
console.log(`attempt ${i} ${success ? 'was successful' : 'failed'}`)
},
})
console.log('val', val)
} catch (err) {
console.error('function timed out')
}
// repeat until
try {
const val = await tryUntil(
() => Math.random(),
(val) => val < 0.1,
{
interval: 100,
numAttempts: 10,
onAttempt: (result, i, success) => {
console.log('received', result)
console.log(`attempt ${i} ${success ? 'was successful' : 'failed'}`)
},
},
)
console.log('val', val)
} catch (err) {
console.error('no value smaller than 0.1 produced')
}
}
```
[npm-url]: https://npmjs.org/package/tryfunc
[downloads-image]: http://img.shields.io/npm/dm/tryfunc.svg
[npm-image]: http://img.shields.io/npm/v/tryfunc.svg
[david-dm-url]:https://david-dm.org/inker/tryfunc
[david-dm-image]:https://david-dm.org/inker/tryfunc.svg
[david-dm-dev-url]:https://david-dm.org/inker/tryfunc#info=devDependencies
[david-dm-dev-image]:https://david-dm.org/inker/tryfunc/dev-status.svg