A promise stands for a value that will arrive later: a server response, a file, a timer. It is pending at first, then either fulfilled with a value or rejected with an error.
async and await
An async function always returns a promise. Inside it, await pauses that function (not the whole program) until the promise settles.
const wait = (ms, value) => new Promise(resolve => setTimeout(() => resolve(value), ms));
const fail = ms => new Promise((_, reject) => setTimeout(() => reject(new Error('server down')), ms));
async function loadProfile() {
const name = await wait(30, 'Asha');
const branch = await wait(30, 'CSE');
return `${name} (${branch})`;
}
async function main() {
console.log(await loadProfile());
try { await fail(10); } catch (e) { console.log('caught:', e.message); }
const start = Date.now();
const [a, b] = await Promise.all([wait(100, 'marks'), wait(100, 'attendance')]);
console.log(a, b, Date.now() - start < 180 ? 'ran in parallel' : 'ran one after another');
}
main();Asha (CSE)
caught: server down
marks attendance ran in parallelRules worth remembering
- Wrap awaits in try/catch, or add .catch() to the promise. An unhandled rejection can crash a Node.js process.
- Two awaits in a row run one after the other. If the tasks do not depend on each other, start them together with Promise.all, as above.
- Promise.all rejects as soon as any task fails. Use Promise.allSettled when you want every result, failed or not.
- await only works inside an async function, or at the top level of an ES module.
How it fits with the event loop
Code after an await runs later as a microtask, after the current synchronous code finishes. See the Node.js event loop tip for the exact order. Practise more in UniCircle interview prep: JavaScript and TypeScript.
#javascript#promises#async await#error handling
Sources and further reading
Practise this as an interview question
Written by the UniCircle team, checked by an editor on 22 Oct 2026. It is not official policy of any university. Exam patterns and rules change; confirm dates and rules on the official website.
