> function doLater(callback, token) {
> var handle = setImmediate(callback);
> token.register(() => clearImmediate(handle));
> }
>
> var cts = new CancellationTokenSource();
> doLater(..., cts.token);
> cts.cancel();
>
I'm not really clear about the ordering properties of setImmediate relative
to the promise microtask queue, but:
function doLater(callback, cancelPromise) {
let handle;
cancelPromise.then(_=> clearImmediate(handle));
handle = setImmediate(callback);
}
This should work if setImmediate uses either the same MT queue as promises
or a lower-priority queue.