question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow waitFor() return an AsyncIterator

See original GitHub issue

What 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
    }
}

Run the Proof-of-Concept

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
bsorrentinocommented, Jul 17, 2022

hi @garronej

Sorry about that, I just released a patch. I do have unit test but somehow this very obvious bug slipped through.

A wise man said me once “Only men that do nothing never make mistakes” 😉

your patch works like a charm 👍

1reaction
bsorrentinocommented, Jul 17, 2022

Hi @garronej

I’ve noted that timeout seems that don’t restart to wait after receive a message. take a look here

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found