Concepts 🚀 Project 🧮 Labs
📚 Learn / Frontend / fetch() & async/await

fetch() & async/await

Call an API, await the JSON, update the DOM. No jQuery, no axios — just fetch.

Code example

async function runCell(cellId) {
  const res = await fetch('/api/notebook/cell', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({code: cell.source})
  });
  const data = await res.json();
  renderOutput(cell, data);
}

How it works

fetch() is the browser's built-in HTTP client. It returns a Promise that resolves to the Response. await unwraps the Promise so you can write async code that reads top-to-bottom. The notebook's runCell function is a textbook example: POST to the API, await the JSON, render the result. Always handle errors with try/catch (or .catch()) — a network blip will reject the promise and crash your render function otherwise.

🔍 Where this is used in OpenBenchML templates/notebook.html — runCellServer, installPackage, resetKernel, refreshKernelInfo all use fetch.