interruptibleChain
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
import { $chain } from "https://esm.town/v/postpostscript/interruptibleChain";
const multiply = $chain()
.then(() => 1)
.then((x) => x + 3)
.then(async (x) => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return x * 2;
});
const handle = multiply.init();
console.log(await handle.untilDone());
// { done: true, value: 8, handle: InterruptibleChainHandle { ... } }
const handle2 = multiply.init({
index: 2,
value: 4,
});
await handle2.advance();
console.log(handle2.done, handle2.state);
// true { index: 3, value: 8 }
/**** Interrupts ****/
const handle3 = $chain().then(() => {
throw new Error("failed");
}).init();
await handle3.advance();
console.log(handle3.done, handle3.interrupt);
// false Interrupt { reason: Error: failed, chain: InterruptibleChain { index: 0 } }
console.log(await handle3.untilDone());
// { done: false, interrupt: Interrupt { ... }, handle: InterruptibleChainHandle { ... } }
const handle4 = $chain().then((_, interrupt) => {
return interrupt("call me later");
}).init();
await handle4.advance()
console.log(handle4.done, handle4.interrupt);
// false Interrupt { reason: "call me later", chain: InterruptibleChain { index: 0 } }
Migrated from folder: Archive/interruptibleChain