Skip to content
Omni Academy
Search courses and lessons
Search
Courses
Backend Development
9
Databases
1
Frontend Development
5
Data Science
5
All courses →
Playground
HTML & CSS
JavaScript
TypeScript
Python
PHP
Ruby
Light
Dark
Auto
Sign in
Get started
All
Backend Development
Databases
Frontend Development
Data Science
Playground
Blog
HTML
JS
TS
PY
PHP
RB
← Courses
📄
Promises and async
Run as I type
Reset
▶
Run
Ctrl
↵
JS
JavaScript source code
// Top-level await works here: this runs as a module. const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); async function fetchScore(name) { await wait(50); if (name === 'nobody') throw new Error('No such learner'); return { name, score: name.length * 12 }; } console.log('Starting...'); // Sequential: each await waits for the one before it. const one = await fetchScore('Aisha'); console.log('One at a time:', one); // Parallel: all three start together, so this takes 50ms, not 150ms. const many = await Promise.all(['Kenji', 'Lena', 'Sam'].map(fetchScore)); console.log('All at once:', many.map((m) => m.name)); // allSettled never rejects - it reports what happened to each one. const mixed = await Promise.allSettled([fetchScore('Lena'), fetchScore('nobody')]); console.log(mixed.map((m) => m.status)); try { await fetchScore('nobody'); } catch (error) { console.error('Caught:', error.message); }
Output
Rendered by your browser