UNPKG

race-cancellation

Version:

Utilities for using Promise.race([task, cancellation]) for async/await code.

26 lines 933 B
import newRaceCancel from "./newRaceCancel.js"; /** * Create a tuple of {@link RaceCancelFn} and {@link ResolveCancelFn} functions. * * @remarks * In general, it is better to use {@link race-cancellation#withCancel} to scope cancellation * to an async task so that the cancellation concern is cleaned up. * * If the cancellation concern will be GC'ed with the cancel already and no cleanup * needed like removing an event listener than this method can be simpler. * @public */ export default function deferCancel() { let cancelled = false; let onCancel; let cancelReason; const raceCancel = newRaceCancel(() => cancelled, (cancel) => (onCancel = cancel), () => cancelReason); const cancel = (reason) => { cancelled = true; cancelReason = reason; if (onCancel !== undefined) onCancel(); }; return [raceCancel, cancel]; } //# sourceMappingURL=deferCancel.js.map