LearnFrenzy
Sign in Take a free test

Array Methods · Find the Error · Question 4

Error: await inside a normal callback Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
async function loadAll(ids) {
  return ids.map((id) => await fetchUser(id));
}
  • A It returns the users
  • B SyntaxError: await is only valid inside an async function, and the arrow callback is not async – use async (id) => … with Promise.all Correct
  • C It returns an array of promises
  • D ReferenceError: fetchUser is not defined
Correct Answer: Option B

Being inside an async function is not enough; the await belongs to the arrow function. Write return Promise.all(ids.map(async (id) => fetchUser(id))) – or simply ids.map(fetchUser).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links