Skip to content

Commit

Permalink
Fix asyncExitHook API documentation and example (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
SCG82 and sindresorhus committed Dec 9, 2022
1 parent 1779ccf commit 7b1545c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ throw new Error('🦄');
//=> 'Exiting'
// Removing an exit hook:
const unsubscribe = asyncExitHook(() => {}, {});
const unsubscribe = asyncExitHook(() => {}, {minimumWait: 500});
unsubscribe();
```
Expand All @@ -77,7 +77,9 @@ import {asyncExitHook, gracefulExit} from 'exit-hook';
asyncExitHook(() => {
console.log('Exiting');
}, 500);
}, {
minimumWait: 500
});
// Instead of `process.exit()`
gracefulExit();
Expand Down
38 changes: 21 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,42 @@ Returns a function that removes the hook when called.

#### onExit

Type: `function(): void`
Type: `() => void`

The callback function to execute when the process exits.

### asyncExitHook(onExit, minimumWait)
### asyncExitHook(onExit, options)

Register a function to run during `gracefulExit`.

Returns a function that removes the hook when called.

Please see [Async Notes](#asynchronous-exit-notes) for considerations when using the asynchronous API.

#### onExit

Type: `() => (void | Promise<void>)`

The callback function to execute when the process exits via `gracefulExit`, and will be wrapped in `Promise.resolve`.

#### options

Type: `object`

##### minimumWait

Type: `number`

The amount of time in milliseconds that the `onExit` function is expected to take.

```js
import {asyncExitHook} from 'exit-hook';

asyncExitHook(async () => {
console.log('Exiting');
}, 300);
}, {
minimumWait: 300
});

throw new Error('🦄');

Expand All @@ -90,20 +108,6 @@ const unsubscribe = asyncExitHook(async () => {
unsubscribe();
```

#### onExit

Type: `function(): void | Promise<void>`

The callback function to execute when the process exits via `gracefulExit`, and will be wrapped in `Promise.resolve`.

#### options

##### minimumWait

Type: `number`

The amount of time in milliseconds that the `onExit` function is expected to take.

### gracefulExit(signal?: number): void

Exit the process and make a best-effort to complete all asynchronous hooks.
Expand Down

0 comments on commit 7b1545c

Please sign in to comment.