CancellationTokenSource implementation for JavaScript based on the C# implementation of the same name
npm install cancellation-token-source
import {CancellationTokenSource} from "cancellation-token-source";
async function myAsyncFunc(cancellationToken: CancellationToken): Promise<void> {
const result = await someOtherThing();
cancellationToken.throwIfCancellationRequested();
// Perform things we don't want to happen when canceled
}
const cts = new CancellationTokenSource();
myAsyncFunc(cts.token);
See the API docs