lib/tools/wait.js
async function waitForEvent(env, event, breakerFunction, timeout = 5000, interval = 10) {
    const { type, urlPattern } = event;
    let intervalId, timeoutId;
    await new Promise((resolve, reject) => {
        const callback = {
            fn: ({ error }) => {
                clearTimeout(timeoutId);
                clearInterval(intervalId);
                if (error) {
                    reject(error);
                } else {
                    resolve();
                }
            },
            urlPattern,
        };

        timeoutId = setTimeout(() => {
            env.removeCallback(type, callback);
            clearInterval(intervalId);
            reject(new Error('Page navigation timeout'));
        }, timeout);

        intervalId = setInterval(() => {
            if (breakerFunction()) {
                clearTimeout(timeoutId);
                clearInterval(intervalId);
                env.removeCallback(type, callback);
                reject(new Error('Function was terminated by breaker'))
            }
        }, interval);

        env.addCallback(type, callback);
    });

    if (type === 'navigation') {
        await env._injectFiles(env._getVendors());
    }
}
async function waitForEvent(env, event, breakerFunction, timeout = 5000, interval = 10) {
    const { type, urlPattern } = event;
    let intervalId, timeoutId;
    await new Promise((resolve, reject) => {
        const callback = {
            fn: ({ error }) => {
                clearTimeout(timeoutId);
                clearInterval(intervalId);
                if (error) {
                    reject(error);
                } else {
                    resolve();
                }
            },
            urlPattern,
        };

        timeoutId = setTimeout(() => {
            env.removeCallback(type, callback);
            clearInterval(intervalId);
            reject(new Error('Page navigation timeout'));
        }, timeout);

        intervalId = setInterval(() => {
            if (breakerFunction()) {
                clearTimeout(timeoutId);
                clearInterval(intervalId);
                env.removeCallback(type, callback);
                reject(new Error('Function was terminated by breaker'))
            }
        }, interval);

        env.addCallback(type, callback);
    });

    if (type === 'navigation') {
        await env._injectFiles(env._getVendors());
    }
}
async function waitForEvent ( env , event , breakerFunction , timeout = 5000 , interval = 10 ) { const { type , urlPattern } = event ; let intervalId , timeoutId ; await new Promise ( ( resolve , reject ) => { const callback = { fn : ( { error } ) => { clearTimeout ( timeoutId ) ; clearInterval ( intervalId ) ; if ( error ) { reject ( error ) ; } else { resolve ( ) ; } } , urlPattern , } ; timeoutId = setTimeout ( ( ) => { env . removeCallback ( type , callback ) ; clearInterval ( intervalId ) ; reject ( new Error ( 'Page navigation timeout' ) ) ; } , timeout ) ; intervalId = setInterval ( ( ) => { if ( breakerFunction ( ) ) { clearTimeout ( timeoutId ) ; clearInterval ( intervalId ) ; env . removeCallback ( type , callback ) ; reject ( new Error ( 'Function was terminated by breaker' ) ) } } , interval ) ; env . addCallback ( type , callback ) ; } ) ; if ( type === 'navigation' ) { await env . _injectFiles ( env . _getVendors ( ) ) ; } }
Wait until event happens
@param {ChromeEnvironment} env
@param {Object} event
@param {Function} [breakerFunction]
@param {number} [timeout]
@param {number} [interval]
@returns {Promise}
Wait until event happens