Skip to content

Commit 62d28c8

Browse files
Document Promise handling in JavaScript/TypeScript code execution
Clarifies that JS/TS code returning Promises is automatically awaited, ensuring users understand they receive resolved values in results. Related to cloudflare/sandbox-sdk#246
1 parent ce0d3ab commit 62d28c8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/content/docs/sandbox/api/interpreter.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ console.log(result.results[0].text); // "15"
9090
</TypeScriptExample>
9191
:::
9292

93+
**Promise handling in JavaScript/TypeScript**:
94+
95+
When executing JavaScript or TypeScript code, if the last expression evaluates to a Promise, it is automatically awaited. This allows you to write async code and receive resolved values in results.
96+
97+
<TypeScriptExample>
98+
```
99+
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
100+
101+
const result = await sandbox.runCode(`
102+
async function fetchData() {
103+
return { status: 'success', value: 42 };
104+
}
105+
fetchData()
106+
`, { context: ctx });
107+
108+
console.log(result.results[0].json); // { status: 'success', value: 42 }
109+
```
110+
</TypeScriptExample>
111+
93112
**Error handling**:
94113

95114
<TypeScriptExample>

0 commit comments

Comments
 (0)