race-cancellation
Version:
Utilities for using Promise.race([task, cancellation]) for async/await code.
22 lines • 1.03 kB
JavaScript
import composeRaceCancel from "./composeRaceCancel.js";
import deferCancel from "./deferCancel.js";
/**
* Wrap a {@link CancellableAsyncFn} function to cancel still pending concurrent child {@link CancellableAsyncFn}
* functions when another concurrent promise either rejected in a Promise.all() or won in a Promise.race().
*
* @param cancellableAsync - a {@link CancellableAsyncFn} function to run with cancel pending
* @param outerRaceCancel - an optional outer {@link RaceCancelFn} function
* @public
*/
export default async function withCancelPending(cancellableAsync, outerRaceCancel) {
const [raceSettled, settled] = deferCancel();
let result;
try {
result = await cancellableAsync(composeRaceCancel(raceSettled, outerRaceCancel));
}
finally {
settled("The operation was cancelled because it was still pending when another concurrent promise either rejected in a Promise.all() or won in a Promise.race().");
}
return result;
}
//# sourceMappingURL=withCancelPending.js.map