# steadfast

> Reliable handling of perpetually pending promises

Latest version **0.1.0** (published 2015-04-03) · ISC license · 0 weekly downloads

## Install

```sh
npm install steadfast
pnpm add steadfast
yarn add steadfast
bun add steadfast
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2015-04-03 |
| First published | 2015-04-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mark Wubben |
| Maintainers | novemberborn |
| Keywords | promise, promises, pending, control-flow |

## Links

- npm: https://www.npmjs.com/package/steadfast
- Repository: https://github.com/novemberborn/steadfast
- Issues: https://github.com/novemberborn/steadfast/issues
- npm.io page: https://npm.io/package/steadfast

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2015-04-03

## README

# steadfast

Reliable handling of perpetually pending promises.

Requires native support for `Promise`, `const` and `let`.

## Installation

```
npm install steadfast
```

## API

### `steadfast.after(promise, delay)`

Allows you to observe whether the `promise` settles within the required time
`delay`, specified in milliseconds.

Returns an object with a new promise that is settled the same way as `promise`,
but remains pending if the timeout is reached first.

The returned object also has an `expired()` method. It takes a `handler`
function and returns a promise. The handler is called if the timeout is reached
before the original `promise` settled. It's passed `resolve` and `reject`
functions that can be used to settle the promise returned when calling
`expired()`. The returned promise is rejected if the handler throws an error.

The returned object also has an `finally()` method. It takes a `handler`
function and has no return value. The handler is called when the timeout is
reached or the original `promise` settles (whichever occurs first).

`setTimeout` is used internally. Consequently the timeout may be triggered a
little sooner or a little later than specified.

The timer is available on the returned object as `timer`. In io.js you could
call [`unref()`](https://iojs.org/api/timers.html#timers_unref) on the timer so
it won't unnecessarily keep the program running. Please read the io.js
documentation carefully, caveats emptor.

Returned promises are created through `new promise.constructor()`, meaning
Steadfast can be used with non-native Promise implementations.

#### Example

```js
const steadfast = require('steadfast');

const pending = new Promise(function(resolve) {
  setTimeout(function() {
    resolve('hi');
  }, 2000);
});

// expired() returns a promise that itself remains pending
steadfast.after(pending, 1000).expired(function() {
  console.log('pending expired after 1 second');
});

// here the promise is fulfilled
steadfast.after(pending, 1000).expired(function(resolve) {
  resolve('expired');
}).then(console.log);

// pending settles before the timeout, so it never expires
steadfast.after(pending, 5000).expired(function() {
  console.log('never called');
});

// pending settles before the timeout, so the promise settles the same way
steadfast.after(pending, 5000).promise.then(console.log);

// pending settles after the timeout, so the promise remains pending
steadfast.after(pending, 1000).promise.then(console.log);

// the handler is called when pending settles before the timeout
steadfast.after(pending, 5000).finally(function() {
  console.log('finally');
});

// the handler is called after the timeout
steadfast.after(pending, 1000).finally(function() {
  console.log('finally');
});

// unref() the timer so the process can exit early
steadfast.after(pending, 5000).timer.unref();
```

## License

ISC

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