LearnFrenzy
Sign in Take a free test

Async/Await · Exercise 3 of 4

Async/Await Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen async/await programs: SyntaxErrors from await outside async code, async constructors and getters, and await as a name; runtime errors from unhandled rejections, using promises as values, bad JSON and missing methods; and silent bugs from missing awaits, sequential awaits, forEach, return without await and promises used as booleans.

Question 11 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
async function getPrice() {
  return 499;
}
async function main() {
  const price = getPrice();
  console.log("Price:", price);
}
main();
A getPrice() is not awaited, so price is the promise itself – write const price = await getPrice()
B async functions cannot return numbers
C console.log cannot print numbers from functions
D main must be awaited
Question page & discussion
Question 12 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const delay = (ms, v) => new Promise((r) => setTimeout(() => r(v), ms));
let inFlight = 0, peak = 0;
const request = async (v) => { peak = Math.max(peak, ++inFlight); await delay(20); inFlight--; return v; };

async function loadDashboard() {
  const profile = await request("profile");      // independent of notifications
  const notifications = await request("notifications");
  console.log(profile, notifications, "| requests at once:", peak);
}
loadDashboard();
A delay must be shorter
B The two independent requests are awaited one after another – start both and await Promise.all to run them in parallel
C request cannot be async
D console.log should be awaited
Question page & discussion
Question 13 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const getPrice = async (item) => ({ pen: 20, book: 250 })[item];
async function cartTotal(items) {
  let total = 0;
  items.forEach(async (item) => {
    total += await getPrice(item);
  });
  return total;
}
cartTotal(["pen", "book"]).then((t) => console.log("Total:", t));
A getPrice returns undefined
B forEach does not wait for the async callbacks, so total is returned before any price is added – use for…of with await (or Promise.all)
C += does not work with await
D total must be const
Question page & discussion
Question 14 Hard

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const loadData = () => Promise.reject(new Error("network error"));
async function safeLoad() {
  try {
    return loadData();
  } catch {
    return "cached data";
  }
}
safeLoad().then((v) => console.log("got", v)).catch((e) => console.log("escaped:", e.message));
A catch without a parameter is invalid
B return loadData() returns the promise without awaiting it, so the rejection happens outside try – write return await loadData()
C loadData must be async
D try/catch never works in async functions
Question page & discussion
Question 15 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
async function isAdmin(user) {
  return user.role === "admin";
}
async function showMenu(user) {
  if (isAdmin(user)) console.log(`${user.name}: admin menu`);
  else console.log(`${user.name}: user menu`);
}
showMenu({ name: "Ravi", role: "student" });
A role must be compared with ==
B async functions cannot return booleans
C The else branch needs braces
D isAdmin returns a promise, and any promise is truthy – write if (await isAdmin(user))
Question page & discussion
Up next · Exercise 4Async/Await Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links