【问题标题】:async/await not working with promise chainingasync/await 不适用于 Promise 链
【发布时间】:2018-07-10 05:34:44
【问题描述】:

我的代码如下所示:

await enterName();
await enterCity();
await submit();

async enterName() {
element.sendKeys('name');
}
async submit() {
element.submit();
waitForAngular();
}

这里的问题是当调用提交函数(异步)时,它根本无法进行任何网络调用。 我尝试使用 controlFlow,但它似乎只有在我在提交之前获得 url 时才有效。 protractor.promise.controlFlow.execute( driver.get(url)); await submit(); ----------> 有效!

但是我的要求是先打开这个网址,填写表格,然后进行异步提交调用。有人可以帮忙吗?

【问题讨论】:

    标签: typescript async-await protractor


    【解决方案1】:

    您需要等待所有返回承诺的函数,包括来自量角器的函数:

    class x {
        element = element(".test");
        async enterName() {
            await this.element.sendKeys('name');
        }
    
        async enterCity() {
            return Promise.resolve();
        }
    
        async waitForAngular() {
            return Promise.resolve();
        }
        async submit() {
            await this.element.submit();
            await this.waitForAngular();  // Assuming this is async as well.
        }
    
        async test() {
            await this.enterName();
            await this.enterCity();
            await this.submit();
        }
    }
    

    【讨论】:

      【解决方案2】:
      1. 要与async/await 合作,您必须disable Control Flow
      2. 函数应该是async,前提是你应该在内部使用await处理一些承诺。

      【讨论】:

        猜你喜欢
        • 2018-08-23
        • 2018-03-24
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 2019-10-24
        • 2015-09-26
        • 2021-06-18
        • 2019-05-30
        相关资源
        最近更新 更多