Skip to content

Commit

Permalink
Expose TimeoutError (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjlescano committed Jun 6, 2020
1 parent 5116832 commit d4c9959
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/// <reference lib="esnext"/>

declare class TimeoutErrorClass extends Error {
readonly name: 'TimeoutError';
constructor(message?: string);
}

declare namespace pEvent {
type TimeoutError = TimeoutErrorClass;

type AddRemoveListener<EventName extends string | symbol, Arguments extends unknown[]> = (
event: EventName,
listener: (...arguments: Arguments) => void
Expand Down Expand Up @@ -251,6 +258,8 @@ declare const pEvent: {

// TODO: Remove this for the next major release
default: typeof pEvent;

TimeoutError: typeof TimeoutErrorClass;
};

export = pEvent;
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,5 @@ module.exports.iterator = (emitter, event, options) => {
}
};
};

module.exports.TimeoutError = pTimeout.TimeoutError;
18 changes: 18 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ Default: `[]`

Events that will end the iterator.

### pEvent.TimeoutError

Exposed for instance checking and sub-classing.

Example:

```js
const pEvent = require('p-event');

try {
await pEvent(emitter, 'finish');
} catch (error) {
if (error instanceof pEvent.TimeoutError) {
// Do something specific for timeout errors
}
}
```

## Before and after

```js
Expand Down

0 comments on commit d4c9959

Please sign in to comment.