Skip to content
Open
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
34 changes: 34 additions & 0 deletions src/content/docs/sandbox/api/interpreter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,40 @@ if (result.error) {
```
</TypeScriptExample>

**JavaScript and TypeScript features**:

JavaScript and TypeScript code execution supports top-level `await` and persistent variables across executions within the same context.

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

// Execution 1: Fetch data with top-level await
await sandbox.runCode(`
const response = await fetch('https://api.example.com/data');
const data = await response.json();
`, { context: ctx });

// Execution 2: Use the data from previous execution
const result = await sandbox.runCode('console.log(data)', { context: ctx });
console.log(result.logs.stdout); // Data persists across executions
```
</TypeScriptExample>

Variables declared with `const`, `let`, or `var` persist across executions, enabling multi-step workflows:

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

await sandbox.runCode('const x = 10', { context: ctx });
await sandbox.runCode('let y = 20', { context: ctx });
const result = await sandbox.runCode('x + y', { context: ctx });

console.log(result.results[0].text); // "30"
```
</TypeScriptExample>

### `listCodeContexts()`

List all active code execution contexts.
Expand Down
Loading