Skip to content

Commit 1b9cb71

Browse files
feat: [workflows] clarify when dynamic step names can be used (#17993)
* feat: [workflows] clarify when dynamic step names can be used * Apply suggestions from code review Co-authored-by: Sidhartha Chatterjee <[email protected]> --------- Co-authored-by: Sidhartha Chatterjee <[email protected]>
1 parent e0f4c2c commit 1b9cb71

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ export class MyWorkflow extends WorkflowEntrypoint {
227227
228228
### Name steps deterministically
229229
230-
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.
230+
Steps should be named deterministically (even if dynamic). This ensures that their state is cached, and prevents the step from being rerun unnecessarily. Step names act as the "cache key" in your Workflow.
231+
231232
232233
```ts
233234
export class MyWorkflow extends WorkflowEntrypoint {
@@ -243,7 +244,20 @@ export class MyWorkflow extends WorkflowEntrypoint {
243244
// Return dynamic values in your state, or log them instead.
244245
let state = await step.do("fetch user data from KV", async () => {
245246
let userData = await env.KV.get(event.user)
246-
console.log(`fetched at ${Date.now})
247+
console.log(`fetched at ${Date.now}`)
247248
return userData
248249
})
250+
251+
// ✅ Good: steps that are dynamically named are constructed in a deterministic way.
252+
// In this case, `catList` is a step output, which is stable, and `catList` is
253+
// traversed in a deterministic fashion (no shuffles or random accesses) so,
254+
// it's fine to dynamically name steps (e.g: create a step per list entry).
255+
let catList = await step.do("get cat list from KV", async () => {
256+
return await env.KV.get("cat-list")
257+
})
258+
for(const cat of carList) {
259+
await step.do(`get cat: ${cat}`, async () => {
260+
return await env.KV.get(cat)
261+
})
262+
}
249263
```

0 commit comments

Comments
 (0)