Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jameslnewell/cancelable-promise
A cancelable promise implementation.
https://github.com/jameslnewell/cancelable-promise
cancelable promise
Last synced: 22 days ago
JSON representation
A cancelable promise implementation.
- Host: GitHub
- URL: https://github.com/jameslnewell/cancelable-promise
- Owner: jameslnewell
- Created: 2018-05-27T23:51:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-05-28T00:36:53.000Z (over 6 years ago)
- Last Synced: 2024-11-08T13:51:26.562Z (3 months ago)
- Topics: cancelable, promise
- Language: TypeScript
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @jameslnewell/cancelable-promise
A cancelable promise implementation.
##### Why?
Unlike other libraries:
* its your choice whether promises are resolved OR rejected
* `.cancel()` is chainable e.g. `promise.then(...).cancel()`## Installation
```
yarn add @jameslnewell/cancelable-promise
```## Usage
```typescript
import CancelablePromise from '@jameslnewell/cancelable-promise';const start = Date.now();
function delay() {
return new CancelablePromise((resolve, reject) => {
const timeout = setTimeout(resolve, 10000);
return () => {
clearTimeout(timeout);
resolve();
};
});
}function exit() {
process.stdin.end();
const finish = Date.now();
const elapsed = (finish - start) / 1000;
console.log(`Waited ${elapsed} seconds.`);
}console.log('Waiting 10s before exiting. Press RETURN to exit now.');
const waiting = delay().then(exit, exit);
process.stdin.on('data', data => {
waiting.cancel();
});
```