Use for await to collect values from an async generator.
Acceptance criteria
- 1.#out should show 1,2,3.
JavaScript Tutorial · JavaScript Generators & Iterators
These 10 tasks go with the JavaScript Async Iteration chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “for await of loops over values that arrive one at a time, each behind a promise.” .
1.Consume an async generatorexercise · easy · 10 min
Use for await to collect values from an async generator.
Passes when: #out should show 1,2,3.
2.Transform each awaited valueexercise · easy · 10 min · practice plan
Uppercase each item as it is awaited and yielded.
Passes when: #out should show A,B.
3.Catch an error mid-streamexercise · easy · 10 min · practice plan
Catch a rejection that happens partway through an async generator.
Passes when: #out should show 1,caught: stream failed.
4.Mix plain and awaited yieldsexercise · easy · 10 min · practice plan
Yield some values immediately and one through a promise.
Passes when: #out should show 1,2,3.
5.Use for await on a plain generatorexercise · medium · 10 min · practice plan
Show that for await also works on a synchronous generator.
Passes when: #out should show 1,2.
6.Fix for of used on an async generatorbug fix · easy · 8 min
An async generator needs for await, not a plain for of.
Passes when: #out should show 1,2,3.
7.Fix the missing async on the generatorbug fix · easy · 8 min · practice plan
await is only a keyword inside an async function, so using it in a plain generator will not even parse.
Passes when: #out should show 1,2,3.
8.Fix the rejection never caughtbug fix · easy · 8 min · practice plan
Without a try block, the loop's error escapes unhandled.
Passes when: #out should show caught it.
9.Fix for await used outside an async functionbug fix · easy · 8 min · practice plan
for await can only be used inside an async function.
Passes when: #out should show 1,2,3.
10.Fix the values pushed before awaitingbug fix · easy · 8 min · practice plan
The value should be pushed after it resolves, not the raw promise.
Passes when: #out should show A,B.
Between them these tasks cover async function*, for await and Async generators.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.