https://github.com/xotic750/attempt-x
Invokes function, returning an object of the results.
https://github.com/xotic750/attempt-x
attempt browser javascript nodejs utility
Last synced: 11 months ago
JSON representation
Invokes function, returning an object of the results.
- Host: GitHub
- URL: https://github.com/xotic750/attempt-x
- Owner: Xotic750
- License: mit
- Created: 2017-09-03T20:48:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T23:17:22.000Z (over 3 years ago)
- Last Synced: 2025-07-08T06:04:10.414Z (11 months ago)
- Topics: attempt, browser, javascript, nodejs, utility
- Language: JavaScript
- Size: 2.18 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## attempt-x
Invokes function, returning an object of the results.
### `module.exports(fn, [...args])` ⇒ Object ⏏
This method attempts to invoke the function, returning either the result or
the caught error object. Any additional arguments are provided to the
function when it's invoked.
**Kind**: Exported function
**Returns**: Object - Returns an object of the result.
| Param | Type | Description |
| --------- | --------------------- | ------------------------------------------ |
| fn | function | The function to attempt. |
| [...args] | \* | The arguments to invoke the function with. |
**Example**
```js
import attempt from 'attempt-x';
function thrower() {
throw new Error('Threw');
}
attempt(thrower, 1, 2);
// {
// threw: true,
// value: // Error('Threw') object
// }
function sumArgs(a, b) {
return a + b;
}
attempt(sumArgs, 1, 2);
// {
// threw: false,
// value: 3
// }
const thisArg = [];
function pusher(a, b) {
return this.push(a, b);
}
attempt.call(thisArg, pusher, 1, 2);
// {
// threw: false,
// value: 2
// }
// thisArg => [1, 2];
```