Allow waitFor() return an AsyncIterator
See original GitHub issueWhat problem it solves
The possibility to wait for event’s series like as example time based events
Possible Solution
I’ve considered to create a new method waitForEvents
that return an AsyncIterator
allowing the consumer to consume “events’ serie” using for await
statement.
Pilot implementation
interface EventIterator<T> extends AsyncIterable<T> {
stop(): void
}
function waitForEvents<T>( evt: Evt<T> , timeout?: number ): EventIterator<T> {
let isStopped = false
async function* events() {
while(!isStopped) {
try{
const res = await evt.waitFor( timeout )
yield res
} catch(error) {
console.warn("TIMEOUT!");
isStopped = true
}
}
}
return {
[Symbol.asyncIterator]: () => events(),
stop: () => isStopped = true
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:15 (8 by maintainers)
Top Results From Across the Web
Wait for an async operation inside asyncIterator generator ...
What I want to do is to yield conditionally based on the return value of the pop() method, which I cannot do because...
Read more >Async iteration and generators - The Modern JavaScript Tutorial
To make an object iterable asynchronously: Use Symbol.asyncIterator instead of Symbol.iterator . The next() method should return a promise (to ...
Read more >JavaScript async iterators - Node.js Design Patterns
Let's start with the async iterator protocol: An object is an async iterator if it has a next() method. Every time you call...
Read more >How do JavaScript async iterators work? - Jim Fisher
The consumer must wait for each promise to resolve, then check the done property, before calling .next() again. This is how JavaScript async ......
Read more >Diving into JavaScript's Async Iterables | by Kevin B. Greene
Working on JavaScript's event loop a lot of what we do is wait for something to happen ... const obj = { [Symbol.asyncIterator]()...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
hi @garronej
A wise man said me once “Only men that do nothing never make mistakes” 😉
your patch works like a charm 👍
Hi @garronej
I’ve noted that timeout seems that don’t restart to wait after receive a message. take a look here