# closeout/exit-hook

> Every export of closeout/exit-hook, with its signature and doc comment: asyncExitHook, gracefulExit, exitHook, plus 3 types.

Source: https://closeout.interlace.tools/docs/api/exit-hook

<!-- Generated by scripts/api-reference.ts from the built dist/*.d.ts. Do not edit; run `npx tsx scripts/api-reference.ts`. -->

```ts
import exitHook from 'closeout/exit-hook';
import { asyncExitHook, gracefulExit } from 'closeout/exit-hook';
```

## Functions

### default

The default export, declared as `exitHook`.

Register a synchronous hook. Returns the function that unregisters it.

Declared here and exported at the foot of the file: `exit-hook`'s default *is* its
`exitHook`, so an `import exitHook from 'closeout/exit-hook'` swap only works if ours is a
default too, and this repository's lint wants every export last and grouped.

```ts
function exitHook(onExit: ExitHookCallback): () => void;
```

| Parameter | Type |
| :-- | :-- |
| `onExit` | `ExitHookCallback` |

**Returns** `() => void`

### asyncExitHook

Register an asynchronous hook, bounded by `wait`.

`wait` is required and must be a positive number: `asyncExitHook(fn, {})` and
`asyncExitHook(fn, { wait: 'abc' })` both throw, and both are graded.

```ts
function asyncExitHook(onExit: ExitHookCallback, options?: Partial<AsyncExitHookOptions>): () => void;
```

| Parameter | Type |
| :-- | :-- |
| `onExit` | `ExitHookCallback` |
| `options` (optional) | `Partial<AsyncExitHookOptions>` |

**Returns** `() => void`

### gracefulExit

Exit the way `process.exit()` cannot: asynchronous hooks get to run, and stdio is drained
before the process goes.

The argument is the exit code, and it is named `signal` upstream. Kept as upstream names
it, because a drop-in whose parameter names disagree with the incumbent's `.d.ts` is a
drop-in that breaks the first person to use a named argument in their editor.

```ts
function gracefulExit(signal?: number): void;
```

| Parameter | Type |
| :-- | :-- |
| `signal` (optional) | `number` |

**Returns** `void`

## Interfaces

### AsyncExitHookOptions

```ts
interface AsyncExitHookOptions {
    /**
     * How long this hook may take, in milliseconds. Required and positive — `exit-hook` throws
     * on a missing or non-numeric value and its `type enforcing` case asserts both.
     */
    wait: number;
}
```

## Types

### ExitHookCallback

A hook takes the code the process is about to leave with.

```ts
type ExitHookCallback = (exitCode: number | string) => unknown;
```

### Options

```ts
type Options = AsyncExitHookOptions;
```
