# stubborn-utils

> A small collection of utilities for making functions somewhat resilient against errors.

Latest version **1.0.2** (published 2025-11-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install stubborn-utils
pnpm add stubborn-utils
yarn add stubborn-utils
bun add stubborn-utils
```

## Health

**Score 65/100 (B)** — status: stable.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2025-11-01 |
| First published | 2025-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | fabiospampinato |
| Keywords | stubborn, collection, utilities, functions |

## Links

- npm: https://www.npmjs.com/package/stubborn-utils
- Repository: https://github.com/fabiospampinato/stubborn-utils
- Homepage: https://github.com/fabiospampinato/stubborn-utils#readme
- Issues: https://github.com/fabiospampinato/stubborn-utils/issues
- npm.io page: https://npm.io/package/stubborn-utils

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [@sandsoftwaresolutions/openapi-fixtures](https://npm.io/package/@sandsoftwaresolutions/openapi-fixtures.md) — 0 weekly downloads
- [mockupflo-mcp](https://npm.io/package/mockupflo-mcp.md) — 0 weekly downloads
- [mock-mcp-server](https://npm.io/package/mock-mcp-server.md) — 0 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2025-11-01
- 1.0.1 — 2025-10-26
- 1.0.0 — 2025-10-26

## README

# Stubborn Utils

A small collection of utilities for making functions somewhat resilient against errors.

## Install

```sh
npm install stubborn-utils
```

## Usage

#### `attemptifyAsync`

This function wraps an async function, and in case this wrapped function rejects then the error is automatically caught by the `onError` callback.

```ts
import {attemptifyAsync} from 'stubborn-utils';

// Let's create a wrapped function

const asyncFunction = async () => {
  await someAsyncThing ();
  if ( Math.random () > 0.5 ) {
    throw new Error ( 'Unlucky' );
  } else {
    return 123;
  }
};

const attemptifiedAsyncFunction = attemptifyAsync ( asyncFunction, {
  onError: error => {
    return -1;
  }
});

const result = attemptifiedAsyncFunction (); // => Promise<123 | -1>
```

#### `attemptifySync`

This function wraps a sync function, and in case this wrapped function throws then the error is automatically caught by the `onError` callback.

```ts
import {attemptifySync} from 'stubborn-utils';

// Let's create a wrapped function

const syncFunction = () => {
  if ( Math.random () > 0.5 ) {
    throw new Error ( 'Unlucky' );
  } else {
    return 123;
  }
};

const attemptifiedSyncFunction = attemptifySync ( syncFunction, {
  onError: error => {
    return -1;
  }
});

const result = attemptifiedSyncFunction (); // => 123 | -1
```

#### `retryifyAsync`

This function wraps an async function, and in case this wrapped function rejects then the `isRetriable` callback is called to decide if we should retry calling the function again.

There's also another layer of options before you can actually call the retryified function, that allows you to provide a maximum `timeout` for the retry loop, and an optional `interval` that should approximately pass between retries.

Before the function is retried again a random amount of milliseconds between `0` and `interval` will be waited for.

By default `interval` would be set to `250`.

```ts
import {retryifyAsync} from 'stubborn-utils';

// Let's create a wrapped function

const asyncFunction = async () => {
  await someAsyncThing ();
  if ( Math.random () > 0.5 ) {
    throw new Error ( 'Unlucky' );
  } else {
    return 123;
  }
};

const retryifiedAsyncFunction = retryifyAsync ( asyncFunction, {
  isRetriable: error => {
    return true; // Always retriablein this scenario
  }
});

const result = retryifiedAsyncFunction ({
  timeout: 1_000,
  interval: 100
}); // => Promise<123 | -1>, but Promise<123> with much higher probability
```

#### `retryifySync`

This function wraps a sync function, and in case this wrapped function throws then the `isRetriable` callback is called to decide if we should retry calling the function again.

There's also another layer of options before you can actually call the retryified function, that allows you to provide a maximum `timeout` for the retry loop, and an optional `interval` that should approximately pass between retries.

Before the function is retried again a random amount of milliseconds between `0` and `interval` will be waited for.

By default `interval` would be set to `250`.

```ts
import {retryifySync} from 'stubborn-utils';

// Let's create a wrapped function

const syncFunction = () => {
  if ( Math.random () > 0.5 ) {
    throw new Error ( 'Unlucky' );
  } else {
    return 123;
  }
};

const retryifiedSyncFunction = retryifySync ( syncFunction, {
  isRetriable: error => {
    return true; // Always retriablein this scenario
  }
});

const result = retryifiedSyncFunction ({
  timeout: 1_000,
  interval: 100
}); // => 123 | -1, but 123 with much higher probability
```

## License

MIT © Fabio Spampinato

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