# function-timeout

> Make a synchronous function have a timeout

Latest version **1.0.2** (published 2024-05-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install function-timeout
pnpm add function-timeout
yarn add function-timeout
bun add function-timeout
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2024-05-17 |
| First published | 2022-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 125 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | function, synchronous, timeout, time, out, cancel, expire, abort, regex, regexp, redos, security, func, fn, script, execute, halt, termination |

## Links

- npm: https://www.npmjs.com/package/function-timeout
- Repository: https://github.com/sindresorhus/function-timeout
- Homepage: https://github.com/sindresorhus/function-timeout#readme
- Issues: https://github.com/sindresorhus/function-timeout/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/function-timeout

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2024-05-17
- 1.0.1 — 2023-11-09
- 1.0.0 — 2023-11-04
- 0.1.1 — 2022-06-28
- 0.1.0 — 2022-06-03

## README

# function-timeout

> Make a synchronous function have a timeout

This can be useful if you accept external data and want to ensure processing it does not take too long.

The timeout only works in Node.js. When used in a browser, the function will be wrapped, but never time out.

*I have a [different package](https://github.com/sindresorhus/super-regex) to prevent [ReDoS](https://en.wikipedia.org/wiki/ReDoS) for regexes.*

## Install

```sh
npm install function-timeout
```

## Usage

```js
import functionTimeout, {isTimeoutError} from 'function-timeout';

const generateNumbers = count => {
	// Imagine this takes a long time.
};

const generateNumbersWithTimeout = functionTimeout(generateNumbers, {timeout: 100});

try {
	console.log(generateNumbersWithTimeout(500));
} catch (error) {
	if (isTimeoutError(error)) {
		console.error('Timed out');
	} else {
		throw error;
	}
}
```

## API

### functionTimeout(function, options?)

Returns a wrapped version of the given function that throws a timeout error if the execution takes longer than the given timeout.

#### options

Type: `object`

##### timeout?

Type: `number` *(integer)*

The time in milliseconds to wait before timing out.

Keep in mind that execution time can vary between different hardware and Node.js versions. Set a generous timeout to avoid flakiness.

### isTimeoutError(error)

Returns a boolean for whether the given error is a timeout error.

## Related

- [super-regex](https://github.com/sindresorhus/super-regex) - Make a regular expression time out if it takes too long to execute
- [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a certain amount of time

---
_Source: https://npm.io/package/function-timeout · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
