# @truffle/promise-tracker

> A tool for wrangling async operations that need to complete before the truffle process exits

Latest version **0.1.7** (published 2023-09-07) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @truffle/promise-tracker
pnpm add @truffle/promise-tracker
yarn add @truffle/promise-tracker
bun add @truffle/promise-tracker
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2023-09-07 |
| First published | 2022-06-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | ^16.20 \|\| ^18.16 \|\| >=20 |
| Dependencies | 0 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13915 |
| Maintainers | rizedr, joshuafernandes, cliffoo, kevinbluer, gnidan, haltman, eggplantzzz, fainashalts, cds-amal |
| Keywords | ethereum, etherscan, ipfs, solidity, verify-source, sourcify, compile |

## Links

- npm: https://www.npmjs.com/package/@truffle/promise-tracker
- Repository: https://github.com/trufflesuite/truffle
- Homepage: https://github.com/trufflesuite/truffle/tree/master/packages/promise-tracker#readme
- Issues: https://github.com/trufflesuite/truffle/issues
- npm.io page: https://npm.io/package/@truffle/promise-tracker

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.7 (latest) — 2023-09-07
- 0.1.1-typescript-migrations.0 (typescript-migrations) — 2022-07-21
- 0.1.0-alpha.2 (signTypedData_v4) — 2022-06-24
- 0.1.6 — 2023-06-06
- 0.1.5 — 2022-10-26
- 0.1.4 — 2022-10-12
- 0.1.3 — 2022-08-17
- 0.1.2 — 2022-08-10
- 0.1.1 — 2022-07-21
- 0.1.0 — 2022-06-23
- 0.1.0-alpha.0 — 2022-06-23

## README

# `@truffle/promise-tracker`

This library is used for keeping track of asynchronous work that needs to complete prior to the process exiting.

## Usage

**IMPORTANT** Only use this library as a last resort. Typically you're better off architecting things so that you don't need process-level tracking of outstanding tasks.

Some alternatives to consider before using this module:

- Wherever possible, only make asynchronous calls from within an asynchronous
  context (aka, avoid using `.then` and `.catch` callbacks).
- Implement timeouts for long-running processes using `Promise.race`
- Unref any best-effort/speculative timers
  ([immediate](https://nodejs.org/api/timers.html#immediateunref),
  [timeout](https://nodejs.org/api/timers.html#timeoutunref) to prevent them
  from keeping your process alive when everything else is done

### Tracking asynchronous operations

For the moment promise tracking is implemented as a method decorator, meaning it
_must_ be applied to method declaration on a class.

It can be applied to any method, and it will only add special handling when methods return promises

```ts
import { tracked } from "@truffle/promise-tracker";

class Foo {
  // totally fine, even though it doesn't return a promise
  @tracked
  synchronousBar(): "-" {
    return "-";
  }

  @tracked
  async asyncBar(): Promise<"-"> {
    return "-";
  }

  // this works the same as with `asyncBar`, even though it's not explicitly an
  // async method
  @tracked
  promiseBar(): Promise<"-"> {
    return new Promise<"-">(resolve => resolve("-"));
  }
}
```

### Waiting for tracked operations to complete (async)

```ts
import { waitForOutstandingPromises } from "@truffle/promiseTracker";

let exitCode = 0;

// If no catchHandler is passed, rejected promises are handled silently.
// This is because these promise rejections should already be handled by the
// caller that created the promise.
await waitForOutstandingPromises({ catchHandler: () => (exitCode = 1) });
process.exit(exitCode);
```

### Waiting for tracked operations to complete (synchronous)

```ts
import { waitForOutstandingPromises } from "@truffle/promiseTracker";

let exitCode = 0;

// If no catchHandler is passed, rejected promises are handled silently.
// This is because these promise rejections should already be handled by the
// caller that created the promise.
waitForOutstandingPromises({ catchHandler: () => (exitCode = 1) }).then(() => {
  process.exit(exitCode);
});
```

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