lib/tools/wait.js
async function waitForEvaluate(env, evalFunction, checkerFunction, breakerFunction, args, timeout, interval) {
    args = args || [];
    checkerFunction = checkerFunction || function(result) {
        return !!result
    };

    timeout = timeout || 5000;
    interval = interval || 10;

    let timeoutId, intervalId;
    return new Promise((resolve, reject) => {
        const errorCallback = {
            fn: ({ error }) => {
                clearTimeout(timeoutId);
                clearInterval(intervalId);
                reject(new Error(`Error during wait with args ${args.toString()}, ${error}`));
            },
        };

        timeoutId = setTimeout(() => {
            env.removeCallback('error', errorCallback);
            clearInterval(intervalId);
            reject(new Error(`Timeout for wait with args ${args.toString()}`));
        }, timeout);

        env.addCallback('error', errorCallback);

        const evalArgs = args.slice(0);
        evalArgs.push(evalFunction);
        intervalId = setInterval(() => {
            env.evaluateJs(...evalArgs)
                .then((result) => {
                    if (checkerFunction(result)) {
                        clearTimeout(timeoutId);
                        clearInterval(intervalId);
                        env.removeCallback('error', errorCallback);
                        resolve();
                        return;
                    }
                    if (breakerFunction()) {
                        clearTimeout(timeoutId);
                        clearInterval(intervalId);
                        env.removeCallback('error', errorCallback);
                        reject(new Error('Function was terminated by breaker'));
                    }
                });
        }, interval);
    });
}
async function waitForEvaluate(env, evalFunction, checkerFunction, breakerFunction, args, timeout, interval) {
    args = args || [];
    checkerFunction = checkerFunction || function(result) {
        return !!result
    };

    timeout = timeout || 5000;
    interval = interval || 10;

    let timeoutId, intervalId;
    return new Promise((resolve, reject) => {
        const errorCallback = {
            fn: ({ error }) => {
                clearTimeout(timeoutId);
                clearInterval(intervalId);
                reject(new Error(`Error during wait with args ${args.toString()}, ${error}`));
            },
        };

        timeoutId = setTimeout(() => {
            env.removeCallback('error', errorCallback);
            clearInterval(intervalId);
            reject(new Error(`Timeout for wait with args ${args.toString()}`));
        }, timeout);

        env.addCallback('error', errorCallback);

        const evalArgs = args.slice(0);
        evalArgs.push(evalFunction);
        intervalId = setInterval(() => {
            env.evaluateJs(...evalArgs)
                .then((result) => {
                    if (checkerFunction(result)) {
                        clearTimeout(timeoutId);
                        clearInterval(intervalId);
                        env.removeCallback('error', errorCallback);
                        resolve();
                        return;
                    }
                    if (breakerFunction()) {
                        clearTimeout(timeoutId);
                        clearInterval(intervalId);
                        env.removeCallback('error', errorCallback);
                        reject(new Error('Function was terminated by breaker'));
                    }
                });
        }, interval);
    });
}
async function waitForEvaluate ( env , evalFunction , checkerFunction , breakerFunction , args , timeout , interval ) { args = args || [ ] ; checkerFunction = checkerFunction || function ( result ) { return ! ! result } ; timeout = timeout || 5000 ; interval = interval || 10 ; let timeoutId , intervalId ; return new Promise ( ( resolve , reject ) => { const errorCallback = { fn : ( { error } ) => { clearTimeout ( timeoutId ) ; clearInterval ( intervalId ) ; reject ( new Error ( ` ${ args . toString ( ) } ${ error } ` ) ) ; } , } ; timeoutId = setTimeout ( ( ) => { env . removeCallback ( 'error' , errorCallback ) ; clearInterval ( intervalId ) ; reject ( new Error ( ` ${ args . toString ( ) } ` ) ) ; } , timeout ) ; env . addCallback ( 'error' , errorCallback ) ; const evalArgs = args . slice ( 0 ) ; evalArgs . push ( evalFunction ) ; intervalId = setInterval ( ( ) => { env . evaluateJs ( ... evalArgs ) . then ( ( result ) => { if ( checkerFunction ( result ) ) { clearTimeout ( timeoutId ) ; clearInterval ( intervalId ) ; env . removeCallback ( 'error' , errorCallback ) ; resolve ( ) ; return ; } if ( breakerFunction ( ) ) { clearTimeout ( timeoutId ) ; clearInterval ( intervalId ) ; env . removeCallback ( 'error' , errorCallback ) ; reject ( new Error ( 'Function was terminated by breaker' ) ) ; } } ) ; } , interval ) ; } ) ; }
Wait until function evalFunction expected in checkerFunction result
@param {AbstractEnvironment} env
@param {Function} evalFunction
@param {Function} [checkerFunction]
@param {Function} [breakerFunction]
@param {Array} [args]
@param {number} [timeout]
@param {number} [interval]
@returns {Promise}
Wait until function evalFunction expected in checkerFunction result