isCancelled
isCancelled answers whether the running task has been asked to stop.
Signature
isCancelled(): booleanIt can only be used inside an async function, because the answer is about the task that is running.
Cancellation is cooperative: it takes effect at a suspension point. A task spending a long time computing reaches none, and so is never interrupted. That is a consequence of the design rather than a hole in it, and this is what such a loop uses to stop itself.
tyrus
import { cancel, isCancelled, sleepAsync } from "tyrus:task";
const compute = async (): Promise<i64> => {
let total: i64 = 0;
for (let round: i32 = 0; round < 500; round += 1) {
for (let i: i64 = 0; i < 200_000; i += 1) {
total += i;
}
// Checked after the expensive part and before suspending again, which
// is where a loop can stop itself rather than be interrupted.
if (isCancelled()) {
console.log("stopped itself");
console.log("assert:", "stopped itself");
return total;
}
await sleepAsync(1);
}
return total;
};
const main = async (): Promise<void> => {
const work: Promise<i64> = compute();
cancel(work, "stop");
try {
await work;
} catch (error: Error) {
console.log(error.message);
console.log("assert:", "stop");
}
};Stopping this way is still a cancellation. A value the task returns after seeing the request does not become the Promise's value: work that was already told to stop does not get to report a result.
