All docs / Build
Workflows
Multi-step jobs that survive retries, waits and restarts, written as ordinary code.
Open in the consoleA workflow is a function whose steps are saved as they finish. If a step fails it is retried; if the node restarts, the run resumes where it left off. A run can sleep for hours or days, or wait for an outside event, without holding anything open.
Write a workflow
import { WorkflowEntrypoint } from "cloudflare:workers";
export class OrderFlow extends WorkflowEntrypoint {
async run(event, step) {
const order = await step.do("load order", async () => {
return (await this.env.DB.prepare("SELECT * FROM orders WHERE id = ?")
.bind(event.payload.orderId).first());
});
await step.sleep("give them time to pay", "1 hour");
const paid = await step.waitForEvent("payment", { type: "paid", timeout: "1 day" });
await step.do("mark as paid", async () => {
await this.env.DB.prepare("UPDATE orders SET paid = 1 WHERE id = ?")
.bind(order.id).run();
});
}
}step.do(name, fn)runsfnonce and saves its result; on replay the saved result is returned.step.sleep(name, duration)andstep.sleepUntil(name, date)pause the run.