I am really intrigued by the for await…of syntax. I think it has a lot of potential and am curious on examples of use. I’ve found a couple of uses so far:
- Hypermedia APIs needing to fetch one at a time as each request offers the URL for the next request. A for await…of and an async generator can do a series of fetch() requests saving the state of the next URL each time till the server exhausts the chain. Example
- Setting up long lived event dispatchers. Adding an event handler that triggers the next iteration. A for await…of can iterate each time an event is triggered. Example
In Node.js for…await of is often used as a convenient run-to-completion WritableStream which is better then the legacy data
event listener pattern. This is because Readable and Duplex streams implement the Symbol.asyncIterator
interface.
WebStreams, however, do not implement the Symbol.asyncIterator
interface. I’d also be curious to see an implementation of a WriteableStream that exposes a Symbol.asyncIterator
.
What other examples have you seen out there that use the for await…of syntax? Especially in the browser.