Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/woubuc/deferred
A Promise that can be resolved externally
https://github.com/woubuc/deferred
async deferred javascript promise typescript
Last synced: about 1 month ago
JSON representation
A Promise that can be resolved externally
- Host: GitHub
- URL: https://github.com/woubuc/deferred
- Owner: woubuc
- License: mit
- Created: 2020-09-08T08:37:14.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-10-11T13:16:44.000Z (about 3 years ago)
- Last Synced: 2024-11-19T11:10:07.686Z (about 2 months ago)
- Topics: async, deferred, javascript, promise, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@woubuc/deferred
- Size: 89.8 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Deferred
A Promise that can be resolved externally[![npm](https://img.shields.io/npm/v/@woubuc/deferred)](https://www.npmjs.com/package/@woubuc/deferred)
[![MIT licensed](https://img.shields.io/badge/license-MIT-green)](https://github.com/woubuc/deferred/blob/master/LICENSE.txt)
[![install size](https://packagephobia.com/badge?p=@woubuc/deferred)](https://packagephobia.com/result?p=@woubuc/deferred)
![Typescript type definitions included](https://img.shields.io/npm/types/@woubuc/deferred)## Installation
```
yarn add @woubuc/deferred
```The library is written in Typescript to types are included.
## Usage
```typescript
import Deferred from '@woubuc/deferred';// Use it as a regular promise
let promise = new Deferred((resolve, reject) => {
resolve('done');
});// Or resolve it externally
promise.resolve('done');// Or omit the promise body altogether
let deferred = new Deferred();
deferred.resolve('done');
```#### API
```typescript
let deferred = new Deferred(promiseCallback);
```
Creates a new Deferred promise. Takes a `(resolve, reject)` callback just like a regular promise.- ``: Type of the promise resolve value
```typescript
deferred.resolve(value : T);
```
Resolves the promise with `value````typescript
deferred.reject(error ?: any);
```
Rejects the promise with an optional `error`.#### Notes
- Extends the native `Promise` object, so it should be fully compatible with regular promises
- Resolving and rejecting follows the same rules as with regular promises (i.e. a promise can only be settled once)
- Resolving the promise externally will not stop or code inside the constructor callback from executing