Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active February 10, 2022 11:40
Show Gist options
  • Save myobie/5d188929abd02b01948fade5ed1ec224 to your computer and use it in GitHub Desktop.
Save myobie/5d188929abd02b01948fade5ed1ec224 to your computer and use it in GitHub Desktop.
I was testing to see if the next() in an async iterator worked how I suspected, where throws are really Promise.reject() which throws at the for await point.
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'
Deno.test('async iterator throw in next should throw in for scope', async () => {
const results: number[] = []
const numbers = (() => {
let counter = 0
const iterator: AsyncIterator<number> = {
// deno-lint-ignore require-await
async next() {
counter += 1
if (counter <= 5) {
return { done: false, value: counter }
} else {
throw 'above 5'
}
}
}
return { [Symbol.asyncIterator]() { return iterator } }
})()
try {
for await (const num of numbers) {
results.push(num)
}
} catch (e) {
assertEquals(e, 'above 5')
}
assertEquals(results, [1,2,3,4,5])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment