o-promise
v1.3.0
Published
Open Promise π β Micro tool for creating Promise likes Deferred (with AbortController support).
Downloads
3,378
Maintainers
Readme
Open Promise π
Micro tool for creating OpenPromise likes Deferred (with AbortController support).
npm i --save o-promise
Usage
import {createOpenPromise} from 'o-promise';
// 1. Tuple
const [promise, resolve, reject] = createOpenPromise<string>();
// 2. Deferred
const {promise, resolve, reject} = createOpenPromise<number>();
// 3. With executer + return
const open = createOpenPromise(() => {
return '...';
});
open.state; // pending
open.reject('canceled'); // `executer` β won't be called
open.state; // rejected
// 4. With executer + promise
const open = createOpenPromise(() => {
// for example
return fetch('...');
});
// 5. With executer + resolvers
const open = createOpenPromise((resolve, reject) => {
// Your Logic
});
Support AbortController
Open "fetch"
import {createOpenPromise} from 'o-promise';
const openFetch = createOpenPromise((_, __, controller) => {
return fetch('...', {signal: controller.signal});
});
// Reject
openFetch.reject('cacneled');
// Or `AbortController#abort`
openFetch.controller.abort('canceled');