Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/content/docs/sandbox/api/interpreter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,45 @@ console.log(result.results[0].text); // "15"
</TypeScriptExample>
:::

**Async/await and Promise handling (JavaScript and TypeScript)**:

The Code Interpreter automatically awaits Promise results in JavaScript and TypeScript contexts, allowing you to work with async code naturally.

<TypeScriptExample>
```
const ctx = await sandbox.createCodeContext({ language: 'javascript' });

// Async IIFE
const result1 = await sandbox.runCode(`
(async () => {
const value = await Promise.resolve(42);
return { result: value * 2 };
})()
`, { context: ctx });

console.log(result1.results[0].json); // { result: 84 }

// Direct Promise.resolve
const result2 = await sandbox.runCode(
'Promise.resolve({ status: "success" })',
{ context: ctx }
);

console.log(result2.results[0].json); // { status: "success" }

// Nested async operations
const result3 = await sandbox.runCode(`
(async () => {
const a = await Promise.resolve(10);
const b = await Promise.resolve(20);
return { sum: a + b };
})()
`, { context: ctx });

console.log(result3.results[0].json); // { sum: 30 }
```
</TypeScriptExample>

**Error handling**:

<TypeScriptExample>
Expand All @@ -104,6 +143,21 @@ if (result.error) {
```
</TypeScriptExample>

**Async error handling**:

<TypeScriptExample>
```
const result = await sandbox.runCode(
'Promise.reject(new Error("Async error"))',
{ language: 'javascript' }
);

if (result.error) {
console.error(result.error.message); // "Async error"
}
```
</TypeScriptExample>

### `listCodeContexts()`

List all active code execution contexts.
Expand Down
31 changes: 31 additions & 0 deletions src/content/docs/sandbox/guides/code-execution.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,37 @@ console.log(result.output); // "Mean: 3.0"
Context state is lost if the container restarts due to inactivity. For critical data, store results outside the sandbox or design your code to reinitialize as needed.
:::

### Async JavaScript and TypeScript

The Code Interpreter automatically awaits Promise results in JavaScript and TypeScript contexts:

<TypeScriptExample>
```
const context = await sandbox.createCodeContext({
language: 'javascript'
});

// Async functions are automatically awaited
const result1 = await sandbox.runCode(`
(async () => {
const response = await Promise.resolve({ data: [1, 2, 3, 4, 5] });
const sum = response.data.reduce((a, b) => a + b, 0);
return { sum, count: response.data.length };
})()
`, { context: context.id });

console.log(result1.results[0].json); // { sum: 15, count: 5 }

// Promise.resolve works directly
const result2 = await sandbox.runCode(
'Promise.resolve({ status: "complete", timestamp: Date.now() })',
{ context: context.id }
);

console.log(result2.results[0].json); // { status: "complete", timestamp: ... }
```
</TypeScriptExample>

## Handle rich outputs

The code interpreter returns multiple output formats:
Expand Down
Loading