LearnFrenzy
Sign in Take a free test
JS

JavaScript Programming · Topic 21 of 25

Async/Await Questions and Concepts

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Coding Techniques

Async/Await · Study material

return vs return await inside try

Returning a promise from inside try hands it to the caller before it settles, so the local catch never sees its rejection. return await keeps the error inside the try block:

const failing = () => Promise.reject(new Error("network error"));

async function withoutAwait() {
  try { return failing(); }                 // the promise is returned before it rejects – catch never runs
  catch { return "fallback"; }
}
async function withAwait() {
  try { return await failing(); }           // await makes the rejection throw inside try
  catch { return "fallback"; }
}
console.log(await withoutAwait().catch((e) => `escaped: ${e.message}`));
console.log(await withAwait());

Output

escaped: network error
fallback

Async generators and for await…of

An async generator produces values that arrive over time – perfect for paginated APIs and streams. for await…of pulls the next value only when the previous one is processed:

// An async generator yields values that arrive over time; for await…of consumes them
const pages = { 1: ["Asha", "Ravi"], 2: ["Meera"], 3: [] };
const fetchPage = (n) => new Promise((r) => setTimeout(() => r(pages[n]), 5));

async function* allUsers() {
  for (let page = 1; ; page++) {
    const users = await fetchPage(page);
    if (users.length === 0) return;          // no more pages
    yield* users;
  }
}
const names = [];
for await (const name of allUsers()) names.push(name);
console.log(names.join(", "), `(${names.length} users)`);

Output

Asha, Ravi, Meera (3 users)

Patterns at a glance

GoalCode
Steps that depend on each otherconst a = await f(); const b = await g(a);
Independent stepsconst [a, b] = await Promise.all([f(), g()]);
Some may failawait Promise.allSettled([...])
Process a list in orderfor (const x of list) await handle(x);
Process a list in parallelawait Promise.all(list.map(handle))
Timeoutawait Promise.race([work, timeout])
Stream of valuesfor await (const v of source)

Async/await checklist

  • await every promise whose result or error you need.
  • Wrap awaits in try/catch (or add .catch at the call site).
  • Start independent work before awaiting it.
  • Use for…of, not forEach, for sequential async loops.
  • Use return await inside try when the catch must see rejections.
  • Never leave an async function call without handling its rejection.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About JavaScript Programming

Why it matters

  • JavaScript runs every web page and, with Node.js, servers, tools and APIs – it is the most used language in the world
  • Front-end, full-stack and MERN roles test JavaScript first, and many campus coding rounds allow it
  • "Predict the output" questions on hoisting, closures, this, coercion and the event loop are interview favourites
  • Modern ES6+ – let/const, arrow functions, destructuring, promises and async/await – is what companies expect from freshers

Who it is for

  • College students (B.Tech, BCA, MCA, B.Sc CS) learning web development and preparing for semester exams and viva
  • Freshers preparing for campus placements, online assessments and front-end interviews
  • Developers moving to React, Angular, Vue or Node.js who need strong core JavaScript
  • Anyone revising ES6 and asynchronous JavaScript before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs on concepts and ES6 features
  • "Predict the output" puzzles – hoisting, coercion, closures, promises and timers
  • "Find the error" debugging: SyntaxError, ReferenceError, TypeError and silent logic bugs
  • Coding problems with solutions and real output, every one run on Node.js – run any study example in your browser

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture, Cognizant tests
  • Front-end developer interviews
  • MERN / full-stack interviews
  • Online coding assessments
  • University practicals

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, debugging, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links