Promises and async/await Basics
Beginner drills on Promise resolution order, async/await semantics, and the microtask queue. Three short JavaScript snippets you can predict line-by-line.
Question Bank
Easy
JavaScript
3 questions
promises
async-await
fundamentals
quiz
156 views
5
Predict the exact print order. Why does the resolved value print after the synchronous tail?
Examples
Example 1:
Input: console.log('A'); Promise.resolve('B').then(v => console.log(v)); console.log('C')
Output: A, then C, then B
Explanation: The script body runs to completion before the event loop drains the microtask queue. Promise.resolve schedules its then callback as a microtask, even for an already-resolved promise.console.log('A');
Promise.resolve('B').then(v => console.log(v));
console.log('C');What does the async function below return when called: a Promise<42> or the number 42? Explain how await interacts with non-promise values.
Examples
Example 1:
Input: async function get() { const x = await 42; return x; } const result = get();
Output: result instanceof Promise === true; result.then(v => console.log(v)) logs 42
Explanation: Any async function returns a promise. await 42 wraps the non-thenable in Promise.resolve(42), schedules the continuation as a microtask, and resumes with the resolved value.async function get() {
const x = await 42;
return x;
}
const result = get();A rejected promise inside an async function escapes via throw. Show the correct way to catch it with try/catch AND with a .catch chain. Why does an unhandled rejection emit unhandledRejection?
Examples
Example 1:
Input: await failing() wrapped in try/catch; alternatively failing().catch(handler)
Output: both forms log 'boom'; removing both handlers emits unhandledRejection
Explanation: Inside an async function, an awaited rejection becomes a synchronous throw, so try/catch works. From outside, the returned promise is rejected, so .catch works. Unhandled rejections fire the runtime event.async function failing() {
await Promise.reject(new Error('boom'));
}