Skip to content

Commit 33504dc

Browse files
authored
workflows: await await await
1 parent f63960a commit 33504dc

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,34 @@ export default {
323323
};
324324
```
325325
</TypeScriptExample>
326+
327+
### `await` your steps
328+
329+
When calling `step.do` or `step.sleep`, you must ensure you use `await` to avoid introducing bugs and race conditions into your Workflow code.
330+
331+
If you don't call `await step.do` or `await step.sleep`, you create a dangling Promise. This occurs when a Promise is created but not properly `await`ed, leading to potential bugs and race conditions.
332+
333+
This happens when you do not use the `await` keyword or fail to chain `.then()` methods to handle the result of a Promise. For example, calling `fetch(GITHUB_URL)` without awaiting its response will cause subsequent code to execute immediately, regardless of whether the fetch completed. This can cause issues like premature logging, exceptions being swallowed (and not terminating the Workflow), and lost return values (state).
334+
335+
<TypeScriptExample filename="index.ts">
336+
```ts
337+
export class MyWorkflow extends WorkflowEntrypoint {
338+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
339+
// 🔴 Bad: The step isn't await'ed, and any state or errors is swallowed before it returns.
340+
const issues = step.do(`fetch issues from GitHub`, async () => {
341+
// The step will return before this call is done
342+
let issues = await getIssues(event.payload.repoName)
343+
return issues
344+
})
345+
346+
// ✅ Good: The step is correctly await'ed.
347+
const issues = await step.do(`fetch issues from GitHub`, async () => {
348+
let issues = await getIssues(event.payload.repoName)
349+
return issues
350+
})
351+
352+
// Rest of your Workflow goes here!
353+
}
354+
}
355+
```
356+
</TypeScriptExample>

0 commit comments

Comments
 (0)