JavaScript Async/Await Deep Understanding Quiz

JavaScript
0 Passed
0% acceptance

Answer 40 questions on async/await internals: how async functions work, await behavior, try/catch error handling, sequential vs parallel flows, awaiting multiple promises, mixing with raw promises, event loop interaction, return values, pitfalls, and best practices.

40 Questions
~80 minutes
1

Question 1

What does an async function always return?

A
A Promise
B
A synchronous value
C
An iterator
D
A generator
2

Question 2

What does await do to the surrounding async function?

A
Pauses execution until the awaited promise settles, then resumes with the fulfillment value
B
Blocks the entire thread
C
Cancels the promise
D
Runs in parallel automatically
3

Question 3

How are errors handled inside async functions by default?

A
Thrown errors become rejected promises
B
Errors crash the program immediately
C
Errors are ignored
D
Errors convert to console logs
4

Question 4

Why is try/catch commonly used with await?

A
Awaited promises can reject; try/catch captures those errors in synchronous style
B
Await prevents errors
C
Catch is required by async functions
D
Await throws only syntax errors
5

Question 5

What does this async function log?

javascript
async function greet() {
  return 'hi'
}
greet().then(console.log)
A
hi
B
undefined
C
Promise
D
Error
6

Question 6

What happens if you await a non-promise value?

A
Await converts it to a resolved promise and continues immediately
B
Await throws a TypeError
C
Await hangs forever
D
Await executes twice
7

Question 7

How does async/await improve readability over raw promise chains?

A
It allows writing asynchronous code in a top-to-bottom style that resembles synchronous flow
B
It executes synchronously
C
It removes the need for promises
D
It blocks the event loop
8

Question 8

What do async functions return when there’s no explicit return statement?

A
A resolved promise with value undefined
B
Nothing
C
A rejected promise
D
A synchronous undefined
9

Question 9

How does the event loop treat await?

A
Await yields control; the rest of the async function runs later as a microtask
B
Await blocks the event loop thread
C
Await runs in a separate thread
D
Await executes synchronously every time
10

Question 10

Why should async functions avoid returning unhandled rejected promises?

A
Unhandled rejections can crash Node.js or trigger console errors in browsers
B
Rejections always turn into alerts
C
Rejections never propagate
D
Rejections pause the event loop
11

Question 11

What order is logged?

javascript
async function run() {
  console.log('start')
  await Promise.resolve()
  console.log('after await')
}
run()
console.log('outside')
A
start outside after await
B
start after await outside
C
outside start after await
D
after await start outside
12

Question 12

What prints?

javascript
async function add() {
  const a = await Promise.resolve(2)
  const b = await Promise.resolve(3)
  return a + b
}
add().then(console.log)
A
5
B
2
C
3
D
undefined
13

Question 13

How can you run two awaits in parallel?

javascript
async function fetchBoth() {
  const aPromise = fetch('/a')
  const bPromise = fetch('/b')
  const [a, b] = await Promise.all([aPromise, bPromise])
  return [a, b]
}
A
Start both requests without awaiting immediately, then await Promise.all
B
Await a before starting b
C
Use await twice sequentially
D
Promise.all cannot be awaited
14

Question 14

What issue exists here?

javascript
async function load() {
  const user = await fetch('/user')
  const posts = await fetch('/posts?user=' + user.id)
  return { user, posts }
}
A
Requests run sequentially even if they could be parallelized
B
Await throws only errors
C
fetch cannot be awaited
D
The function fails to return a promise
15

Question 15

What does the error handling do?

javascript
async function readJson(url) {
  try {
    const res = await fetch(url)
    return await res.json()
  } catch (err) {
    console.error('Failed', err)
    throw err
  }
}
A
Logs the error and rethrows so callers can handle it
B
Swallows the error
C
Converts the error to success
D
Prevents catch from running
16

Question 16

What is logged?

javascript
async function chain() {
  return await Promise.resolve('value')
}
chain().then(console.log)
A
value
B
Promise
C
undefined
D
Error
17

Question 17

What does this illustrate?

javascript
async function example() {
  await Promise.reject('oops')
  console.log('after reject')
}
example().catch(err => console.log('caught', err))
A
Awaited rejections throw, skipping code until a catch handles the error
B
Await continues despite rejection
C
catch never runs
D
Rejections become console logs automatically
18

Question 18

What race condition can happen here?

javascript
let ready = false
async function setReady() {
  await Promise.resolve()
  ready = true
}
setReady()
console.log(ready)
A
console.log runs before await resolves, so ready is false
B
ready becomes true immediately
C
Await blocks the log
D
ready is undefined
19

Question 19

What prints from parallel awaits?

javascript
async function parallel() {
  const [a, b] = await Promise.all([
    Promise.resolve('A'),
    Promise.resolve('B')
  ])
  console.log(a + b)
}
parallel()
A
AB
B
BA
C
A
D
B
20

Question 20

Why should you avoid awaiting inside Array.prototype.forEach?

javascript
async function process(items) {
  items.forEach(async item => {
    await save(item)
  })
}
A
forEach does not await the async callbacks, so process returns before saves finish
B
Await is illegal inside forEach
C
Async functions cannot be callbacks
D
save throws
21

Question 21

How can you combine async/await with Promise.all for efficiency?

A
Start multiple async tasks, store their promises, then await Promise.all at the end
B
Await each task sequentially
C
Use only callbacks
D
Promise.all cannot be awaited inside async
22

Question 22

How do you handle errors when mixing async/await and .then?

A
Use try/catch around await and still attach .catch on any promise returned from async functions
B
Errors disappear automatically
C
You cannot mix them
D
Use console.log only
23

Question 23

What problem arises when forgetting await?

A
The code continues before the promise resolves, often leading to bugs or unresolved values
B
Await automatically happens
C
Promises reject immediately
D
Async functions stop working
24

Question 24

How can you parallelize operations inside async functions safely?

A
Kick off promises, store them, and await Promise.all or Promise.allSettled
B
Use setTimeout only
C
Use global variables
D
Parallelization is impossible
25

Question 25

Why might sequential awaits hurt performance?

A
Each await waits for the previous to finish, even if tasks are independent
B
Sequential awaits improve speed
C
await blocks other threads
D
Promises cannot run simultaneously
26

Question 26

What best practice applies to async error logging?

A
Log context and rethrow or return errors appropriately so upstream callers know what failed
B
Swallow errors after logging
C
Convert errors to console.warn only
D
Ignore errors to keep logs clean
27

Question 27

How can async functions return resolved data to non-await callers?

A
Return the promise and let callers use .then or await themselves
B
Return synchronous values only
C
Use global state
D
Throw errors always
28

Question 28

What does queueMicrotask have to do with async/await?

A
Awaited resumes run as microtasks similar to queueMicrotask callbacks
B
Async functions use macrotasks only
C
queueMicrotask blocks await
D
They are unrelated
29

Question 29

What is a pitfall of mixing async/await with Array.map?

A
map returns an array of promises that must be awaited with Promise.all
B
map automatically awaits each callback
C
Async callbacks are forbidden in map
D
map turns promises into strings
30

Question 30

Why should long-running synchronous code still be avoided inside async functions?

A
Async functions do not magically make synchronous code non-blocking
B
await pauses synchronous work
C
Async converts sync to parallel
D
Blocking code becomes asynchronous automatically
31

Question 31

What is the best way to retry an async operation?

A
Wrap the await in a loop with try/catch, optionally adding delay/backoff
B
Call await twice in a row
C
Ignore errors and hope for success
D
Use synchronous recursion
32

Question 32

How should you handle cleanup when using await?

A
Use try/finally so cleanup runs whether the awaited operation succeeds or fails
B
Cleanup is unnecessary
C
Cleanup only on success
D
Use finally only with promises
33

Question 33

What is an antipattern when returning promises from async functions?

A
Wrapping the result in new Promise unnecessarily (double wrapping)
B
Returning awaited results
C
Propagating errors with throw
D
Using Promise.all
34

Question 34

How do you ensure sequential order when needed?

A
Await each operation in order, or use for...of with await
B
Use Promise.all
C
Use setTimeout
D
Let operations race
35

Question 35

What does “fire and forget” mean with async functions?

A
Starting an async task without awaiting it; requires manual error handling/logging
B
Awaiting everything
C
Cancelling promises automatically
D
Throwing errors deliberately
36

Question 36

Why is top-level await useful in modules?

A
It lets modules await asynchronous initialization before exporting values
B
It replaces async functions
C
It blocks the browser entirely
D
It runs synchronous code faster
37

Question 37

What is a pitfall when awaiting inside map without return?

A
The map callback returns undefined, so Promise.all receives undefined instead of promises
B
Await throws automatically
C
map converts values to strings
D
There is no pitfall
38

Question 38

How can you avoid deadlocks when awaiting user input or events?

A
Combine await with event listeners or races that include timeouts/cancellation
B
Await the same unresolved promise forever
C
Block the event loop
D
Use synchronous loops
39

Question 39

Why should async functions be composable?

A
Returning promises lets other async functions await them or combine them with Promise.all
B
Async functions must be global
C
Async functions cannot call each other
D
Promises cannot be returned
40

Question 40

What best practice ensures async code remains maintainable?

A
Structure async flows intentionally (sequential vs parallel), handle errors clearly, and document assumptions
B
Avoid logging
C
Mix random awaits
D
Rely on implicit behavior

QUIZZES IN JavaScript