on-error-resume-next
Run a function, synchronously or asynchronously, and ignore errors.
The name come from Visual Basic. The original On Error Resume Next
statement is considered a bad error handling practice.
Although the perception of the feature is negative, when scoped and used responsibly, it can become a very helpful utility function.
Breaking changes
Usage
onErrorResumeNext
will return the result if it is a success. For example,
import onErrorResumeNext from 'on-error-resume-next';
const text = '{"hello":"World!"}';
const parsed = onErrorResumeNext(() => JSON.parse(text));
expect(parsed).toEqual({ hello: 'World!' });
Otherwise, it will return undefined
,
const parsed = onErrorResumeNext(() => JSON.parse('<xml />'));
expect(parsed).toBeUndefined();
When using onErrorResumeNext
, please be responsible and fully understand the impact of ignoring errors.
Asynchronous using async/await
onErrorResumeNext
will capture both exceptions (synchronous) and rejections (asynchronous).
import onErrorResumeNext from 'on-error-resume-next/async';
const res = await onErrorResumeNext(() => fetch('/health'));
await expect(res).resolves.toBeUndefined();
Contributions
Like us? Star us.
Want to make it better? File us an issue.
Don't like something you see? Submit a pull request.