Javascript 如何基于承诺值包装循环、承诺和中断循环

Javascript 如何基于承诺值包装循环、承诺和中断循环,javascript,typescript,protractor,Javascript,Typescript,Protractor,我试图找到一种方法来创建一个自定义的等待条件发生。如下所示: static waitForActivityToHappen(activityName: string, timeout: number) { const startTime = PageHelper.getCurrentTime(); // Get current time in milisecond while ((PageHelper.getCurrentTime() - startTime) < timeou

我试图找到一种方法来创建一个自定义的等待条件发生。如下所示:

static waitForActivityToHappen(activityName: string, timeout: number) {
   const startTime = PageHelper.getCurrentTime(); // Get current time in milisecond
   while ((PageHelper.getCurrentTime() - startTime) < timeout) {
       browser.sleep(PageHelper.timeout.xxs); // Poll every 1 sec
       <Do some action here>
       element.all(By.xpath(xpath)).count().then(function (count) {
        if (count > 0) {
            <break the loop here>
        }
    });
   }
}

但这是行不通的。请告诉我如何实现这一点。

除非使用ES2017+异步函数并等待,否则不能将循环结构与异步代码一起使用。相反,您必须从当前迭代计划下一个迭代。下面是一个这样做的示例:

static waitForActivityToHappen(activityName: string, timeout: number) {
    const startTime = PageHelper.getCurrentTime(); // Get current time in milisecond
    // Start the process
    tick();
    function tick() {
        browser.sleep(PageHelper.timeout.xxs) // Poll every 1 sec
            .then(() => /*...do some action here perhaps...*/)
            .then(() => element.all(By.xpath(xpath)).count())
            .then(count => {
                if (count > 0) {
                    // Done
                } else if ((PageHelper.getCurrentTime() - startTime) < timeout) {
                    // Try again
                    tick();
                } else {
                    // Timed out
                }
            })
            .catch(err => {
                // do something with the error
            });
    }
}

我会尝试使用browser.wait函数,它接受第一个参数作为谓词函数,所以您可以编写任何条件,只要谓词函数返回true/false或Promise,并将其解析为true/false,它就会工作

static waitForActivityToHappen(activityName:string, timeout: number) {
    let waitForActivityToHappenPredicate = function () {
        return element.all(By.xpath(xpath)).count().then(function () {
            if (count > 0) {
                return true
            } else {
                return false
            }
        }, function (err) {return false})
    }

    browser.wait(waitForActivityToHappenPredicate, timeout, 'Some timeout message here')
}

没有browser.sleep这样的东西。但是存在window.setTimeout。除非使用ES2017+异步函数并等待,否则不能将循环结构与异步代码一起使用。相反,您必须使用setTimeout从当前迭代计划下一次迭代,但是如果element.all操作已经是异步的,人为地延迟事情似乎很奇怪。@Pointy:他使用的是量角器,它确实有。他没有正确地使用它,但它确实存在。@T.J.Crowder每天都能学到一些东西。新年快乐@波蒂:新年快乐-
static waitForActivityToHappen(activityName:string, timeout: number) {
    let waitForActivityToHappenPredicate = function () {
        return element.all(By.xpath(xpath)).count().then(function () {
            if (count > 0) {
                return true
            } else {
                return false
            }
        }, function (err) {return false})
    }

    browser.wait(waitForActivityToHappenPredicate, timeout, 'Some timeout message here')
}