
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
race-cancellation
Advanced tools
Utilities for using Promise.race([task, cancellation]) for async/await code.
This is a library with utils to implement a cancellable async function with a pattern that solves a number of issues with cancellation of promises.
The pattern is a cancellable async function takes a function that will build the cancellation race lazily.
export type AsyncFn<T> = () => Promise<T>;
export type RaceCancelFn = <T>(asyncFnOrPromise: AsyncFn<T> | PromiseLike<T>) => Promise<T>;
export type CancellableAsyncFn<T> = (raceCancel: RaceCancelFn) => Promise<T>;
Since race cancellation is a function it can lazily create the Promise to avoid unhandledRejection.
Since it can take a function to invoke to produce the task, it can avoid starting the task if it is already cancelled and can avoid creating the Promise if the task fails on invoke.
This also allows the combined race cancellations to be lazy. For example, if we are already cancelled because we are disconnected we don't need to start a timeout.
import * as fs from "fs";
async function pollFile(path, interval, raceCancel) {
while (!fs.existsSync(path)) {
await sleep(interval, raceCancel);
}
}
async function sleep(ms, raceCancel) {
let id;
try {
const createTimeout = () =>
new Promise<void>(resolve => {
id = setTimeout(resolve, ms);
});
// if cancellation has happened race cancel can return
// a rejected promise without invoking createTimeout
// otherwise createTimeout is called and raced against
// a new Promise<never> created from the cancellation Promise,
// cancellationPromise.then(throwCancellationError)
return await raceCancel(createTimeout);
} finally {
if (id !== undefined) {
// cleanup timeout so node will exit right away if
// our script is done.
clearTimeout(id);
}
}
}
FAQs
Utilities for using Promise.race([task, cancellation]) for async/await code.
We found that race-cancellation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.