|
| 1 | +/* eslint-disable @typescript-eslint/require-await */ |
| 2 | +import { sleep } from "workflow"; |
| 3 | +import { FatalError } from "workflow"; |
| 4 | + |
| 5 | +export async function handleUserSignup(email: string) { |
| 6 | + "use workflow"; |
| 7 | + |
| 8 | + const user = await createUser(email); |
| 9 | + await sendWelcomeEmail(user); |
| 10 | + |
| 11 | + await sleep("5s"); // Pause for 5s - doesn't consume any resources |
| 12 | + await sendOnboardingEmail(user); |
| 13 | + |
| 14 | + return { userId: user.id, status: "onboarded" }; |
| 15 | +} |
| 16 | + |
| 17 | +// Our workflow function defined earlier |
| 18 | + |
| 19 | +async function createUser(email: string) { |
| 20 | + "use step"; |
| 21 | + |
| 22 | + console.log(`Creating user with email: ${email}`); |
| 23 | + |
| 24 | + // Full Node.js access - database calls, APIs, etc. |
| 25 | + return { id: crypto.randomUUID(), email }; |
| 26 | +} |
| 27 | + |
| 28 | +async function sendWelcomeEmail(user: { id: string; email: string }) { |
| 29 | + "use step"; |
| 30 | + |
| 31 | + console.log(`Sending welcome email to user: ${user.id}`); |
| 32 | + |
| 33 | + if (Math.random() < 0.3) { |
| 34 | + // By default, steps will be retried for unhandled errors |
| 35 | + throw new Error("Retryable!"); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +async function sendOnboardingEmail(user: { id: string; email: string }) { |
| 40 | + "use step"; |
| 41 | + |
| 42 | + if (!user.email.includes("@")) { |
| 43 | + // To skip retrying, throw a FatalError instead |
| 44 | + throw new FatalError("Invalid Email"); |
| 45 | + } |
| 46 | + |
| 47 | + console.log(`Sending onboarding email to user: ${user.id}`); |
| 48 | +} |
0 commit comments