Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/susisu/promise-utils

Promise utilities
https://github.com/susisu/promise-utils

Last synced: 1 day ago
JSON representation

Promise utilities

Awesome Lists containing this project

README

        

# @susisu/promise-utils

[![CI](https://github.com/susisu/promise-utils/workflows/CI/badge.svg)](https://github.com/susisu/promise-utils/actions?query=workflow%3ACI)

Promise utilities

``` shell
# npm
npm i @susisu/promise-utils
# yarn
yarn add @susisu/promise-utils
# pnpm
pnpm add @susisu/promise-utils
```

## Usage

### `triplet`

Creates a triplet of a promise and its resolve and reject functions.

``` typescript
import { triplet } from "@susisu/promise-utils";

const [promise, resolve, reject] = triplet();

resolve(42);

promise.then(res => {
console.log(res); // -> 42
});
```

### `attachActions`

Attaches cancellable actions to a promise.

``` typescript
import { triplet, attachActions } from "@susisu/promise-utils";

const [promise, resolve, reject] = triplet();

const [cancel] = attachActions(
promise,
res => { console.log(res); },
err => { console.log(err); }
);

cancel();

resolve(42); // no output
```

## License

[MIT License](http://opensource.org/licenses/mit-license.php)

## Author

Susisu ([GitHub](https://github.com/susisu), [Twitter](https://twitter.com/susisu2413))