Skip to content

Commit 8f5ecba

Browse files
committed
feat: [workflows] clarify when dynamic step names can be used
1 parent 6ab44d4 commit 8f5ecba

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/content/docs/workflows/build/rules-of-workflows.mdx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ export class MyWorkflow extends WorkflowEntrypoint {
229229
230230
Dynamically naming a step will prevent it from being cached, and cause the step to be re-run unnecessarily. Step names act as the "cache key" in your Workflow.
231231
232+
Note that, step names can be formed dynamically as long they are deterministic across steps and/or Workflow restarts.
233+
232234
```ts
233235
export class MyWorkflow extends WorkflowEntrypoint {
234236
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
@@ -243,7 +245,20 @@ export class MyWorkflow extends WorkflowEntrypoint {
243245
// Return dynamic values in your state, or log them instead.
244246
let state = await step.do("fetch user data from KV", async () => {
245247
let userData = await env.KV.get(event.user)
246-
console.log(`fetched at ${Date.now})
248+
console.log(`fetched at ${Date.now}`)
247249
return userData
248250
})
251+
252+
// ✅ Good: steps that are dynamically named are constructed in a deterministic way.
253+
// In this case, `catList` is a step output, which is stable, and `catList` is
254+
// traversed in a deterministic fashion (no shuffles or random accesses) so,
255+
// it's fine to dynamically name steps (e.g: create a step per list entry).
256+
let catList = await step.do("get cat list from KV", async () => {
257+
return await env.KV.get("cat-list")
258+
})
259+
for(const cat of carList) {
260+
await step.do(`get cat: ${cat}`, async () => {
261+
return await env.KV.get(cat)
262+
})
263+
}
249264
```

0 commit comments

Comments
 (0)