Skip to content
RED SAG Skills
hardStudy question · answer published in JavaScript interview questions, so no pointsjavascriptgeneratorsasync-await

Breaking out of for await

An async generator opens a database cursor and closes it in a finally block. What happens when the loop breaks early?

async function* rows() {
  const cur = await db.openCursor()
  try {
    for await (const r of cur) yield r
  } finally {
    await cur.close()
  }
}
for await (const r of rows()) {
  if (r.id === 10) break
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions