Skip to content

Latest commit

 

History

History
42 lines (26 loc) · 1.04 KB

promise-from-signal.md

File metadata and controls

42 lines (26 loc) · 1.04 KB

promiseFromSignal(signal, {rejects})

Returns a Promise that resolves or rejects when the signal is aborted. This lets you await a signal or combine it with other promises.

The promise will use the abort reason as a resolved/rejected value.

import {promiseFromSignal} from 'abort-utils';

// Resolves after 100ms
await promiseFromSignal(AbortSignal.timeout(100));

// It accepts controllers too, and it can reject instead of resolving
const newController = new AbortController();
const promise = promiseFromSignal(newController, {reject: true});

newController.abort(new Error('Rent is too damn high'));

try {
	await promise;
} catch (error) {
	console.log('The signal failed with', error); // Error: Rent is too damn high
}

signal

Type: AbortSignal, AbortController

The signal to listen to. If you pass a controller, it will automatically extract its signal.

options

Type: object

rejects

Type: boolean

If true, the promise will reject instead of resolve when the signal is aborted.