diff --git a/src/content/docs/sandbox/api/interpreter.mdx b/src/content/docs/sandbox/api/interpreter.mdx index 562effd32142ed2..ee33bb4d065f2da 100644 --- a/src/content/docs/sandbox/api/interpreter.mdx +++ b/src/content/docs/sandbox/api/interpreter.mdx @@ -104,6 +104,40 @@ if (result.error) { ``` +**JavaScript and TypeScript features**: + +JavaScript and TypeScript code execution supports top-level `await` and persistent variables across executions within the same context. + + +``` +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 +``` + + +Variables declared with `const`, `let`, or `var` persist across executions, enabling multi-step workflows: + + +``` +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" +``` + + ### `listCodeContexts()` List all active code execution contexts.