ericelliott / ericelliott/speculation

Util for error branching?

Open
#3 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
198
Forks
6
PR merge metrics
No merged PRs in 30d

Description

People think they need separate channels for handling cancellations, but a simple error branching utility could be a better solution. Here's a trivial example:

```js
const handleErrors = (
fn,
onCancel = () => {}
) => e => {
(/Cancelled/.test(e.message)) ?
onCancel(e) : fn(e);
};

const handler = handleErrors(
e => console.log(`Not cancelled: ${ e }`),
c => console.log(`Cancelled: ${ c }`)
);
```

A `handleErrors()` utility could be fleshed out to handle custom error predicates, and to handle any number of different kinds of errors.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.