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.

waitFor custom event feature request

See original GitHub issue

There is no API at the moment to wait for a custom event in the node context, right?

Example:

await page.goto('gridwithimages.com', {waitUntil: 'grid-load'});
await page.screenshot();

I only found custom-event.js, which is quite verbose and doesn’t await until the event is fired anyway.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
john-dohertycommented, Jan 2, 2021

I created a method to simplify this await waitForEvent('app-ready', 10)

/**
 * Wait for the browser to fire an event (including custom events)
 * @param {string} eventName - Event name
 * @param {integer} seconds - number of seconds to wait.
 * @returns {Promise} resolves when event fires or timeout is reached
 */
async function waitForEvent(eventName, seconds) {

    seconds = seconds || 30;

    // use race to implement a timeout
    return Promise.race([

        // add event listener and wait for event to fire before returning
        page.evaluate(function(eventName) {
            return new Promise(function(resolve, reject) {
                document.addEventListener(eventName, function(e) {
                    resolve(); // resolves when the event fires
                });
            });
        }, eventName),

        // if the event does not fire within n seconds, exit
        page.waitFor(seconds * 1000)
    ]);
}
4reactions
joao-condecommented, May 3, 2021

I created a method to simplify this await waitForEvent('app-ready', 10)

/**
 * Wait for the browser to fire an event (including custom events)
 * @param {string} eventName - Event name
 * @param {integer} seconds - number of seconds to wait.
 * @returns {Promise} resolves when event fires or timeout is reached
 */
async function waitForEvent(eventName, seconds) {

    seconds = seconds || 30;

    // use race to implement a timeout
    return Promise.race([
        page.evaluate(function(eventName) {
            return new Promise(function(resolve, reject) {
                document.addEventListener(eventName, function(e) {
                    resolve(); // resolves when the event fires
                });
            });
        }, eventName),

        page.waitFor(seconds * 1000)
    ]);
}

This is very useful, could this be made part of the Puppeteer API?

I refactored to this:

async function waitForEvent(page, event, timeout = 5000) {
     return Promise.race([
         page.evaluate(
             event => new Promise(resolve => document.addEventListener(event, resolve, { once: true })),
             event
         ),
         page.waitForTimeout(timeout)
     ]);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

waitFor custom event feature request · Issue #2455 - GitHub
timeStamp events, someone might want to wait on a specific [CustomEvent] dispatched to a certain node. I'd recommend using helpers for now that ......
Read more >
How to write custom code in for event in wait for event activity?
First Choose the group and the event which you wanted to listen to. Then define the object on which the event will happen....
Read more >
Wait For Custom Event - YouTube
Music: “Extreme Driving” audiojungle.netBackground animation by: AA VFX , Amitai Angor , https://www.youtube.com/dvdangor2011 4K TV DAMAGE ...
Read more >
How to Track Custom Events with Google Analytics 4
In this in-depth guide, you will learn how to track Custom Events with Google Analytics 4 with Google Tag Manager.
Read more >
Trigger: Wait for Event - HCL Product Documentation
This trigger is a “make your own” trigger. You can trigger a dialog activity when a specific event occurs that the existing triggers...
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