Wraps a promise with a cancelable interface.
The returned object contains a promise and a cancel method. If cancel is called before the promise settles, the promise rejects with a "Cancelled" error.
promise
cancel
The type of the promise’s resolved value.
The promise to wrap with cancellation capability.
An object with the wrapped promise and a cancel function.
const { promise, cancel } = withCancel(fetch("/api/data"));setTimeout(() => cancel(), 100); // cancel after 100mspromise.catch(err => console.log(err.message)); // "Cancelled" Copy
const { promise, cancel } = withCancel(fetch("/api/data"));setTimeout(() => cancel(), 100); // cancel after 100mspromise.catch(err => console.log(err.message)); // "Cancelled"
1.1.0
Wraps a promise with a cancelable interface.
The returned object contains a
promise
and acancel
method. Ifcancel
is called before the promise settles, the promise rejects with a "Cancelled" error.