【问题标题】:Why is my method not waiting for the promise to resolve before returning?为什么我的方法在返回之前没有等待承诺解决?
【发布时间】:2018-06-16 22:46:50
【问题描述】:

我正在尝试构建一个填充了商家名称的数组,每个商家名称都有一个元素 ID。我首先检查该行是否有值,因为我不知道列表中有多少商家,除了少于 20 个。我想返回数组,以便对其进行排序并执行其他操作验证。承诺不会在返回之前等待解决。

我可以看到数组构建,但我想输出最终列表Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant

正如您在下面看到的那样,即使我已经建立了一个承诺并正在调用 .then() 以在我的 @ 987654323@ 声明。

merchants.po.ts

export class MerchantsPage {
        buildMerchantListArray(): Promise < string[] > {
            return new Promise < string[] > (resolve => {
                const list: string[] = [];

                for (let i = 20; i >= 0; i--) {
                    this.getMerchantName(i)
                        .isPresent()
                        .then((present) => {
                            if (present) {
                                this.getMerchantName(i)
                                    .getText()
                                    .then((text: string) => {
                                        list.unshift(text);
                                        console.log('building list: ' + list)
                                    })
                            }
                        });
                }

                return resolve(list);

            });
        }

        doBuild() {
            this.buildMerchantListArray()
                .then((list: string[]) => {
                    return console.log('doBuild: ' + list);
                });
        }

        /**
         * Get table element for Merchant's Name of row i
         * @param i
         * @returns {ElementFinder}
         */
        getMerchantName(i: number): ElementFinder {
            return element(by.id('Merchant-' + i + '-Name'));
        }
    }

merchants.e2e-spec.ts

import {FooterParams} from '../../../../components/footer/footer.params';
import {Footer} from '../../../../components/footer/footer.po';
import {Logo} from '../../../utility/logo/logo.po';
import {LoginParams} from '../../../authentication/login/login.params';
import {LoginPage} from '../../../authentication/login/login.po';
import {AppPage} from '../../../../app.po';
import {Header} from '../../../dashboard/header/header.po';
import {ElementFinder} from 'protractor';
import {MerchantsPage} from './merchants.po';
import {MerchantsParams} from './merchants.params';

describe('Merchants Pages of Dashboard App', () => {
    let app: AppPage;
    let login: LoginPage;
    let navigation: Header;
    let page: MerchantsPage;

    beforeAll(() => {
        navigation = new Header();
        app = new AppPage();
        login = new LoginPage();
        page = new MerchantsPage();
        app.navigateTo('dashboard').then(() => {
            login.checkAuthentication(LoginParams.user, LoginParams.user_pwd);
        });
    });

    beforeEach(() => {
        Logo.getLogo().click().then(() => {
            navigation.clickAdminDropdown().then(() => {
                navigation.clickMerchants();
                expect(page.getMerchantsHeader().getText()).toContain(MerchantsParams.merchantsHeaderText);
                Footer.getFooter().then((footer: ElementFinder) => {
                    (expect(footer.getText()).toContain(FooterParams.footer));
                });
            });
        });
    });

    it('should verify the Merchants list view is alphabetical by Name', () => {
        Promise.resolve(page.buildMerchantListArray()).then((arr) => {
            console.log('my list: ' + arr);
        });

        page.doBuild();

        page.buildMerchantListArray().then((response) => {
            console.log('response: ' + response);
        });

    });

});

控制台输出

Jasmine started
my list:
doBuild:
response:
building list: TestMerchant
building list: Paytrace Merchant,TestMerchant
building list: Michelle's Business,Paytrace Merchant,TestMerchant
building list: Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: TestMerchant
building list: Paytrace Merchant,TestMerchant
building list: Michelle's Business,Paytrace Merchant,TestMerchant
building list: Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: TestMerchant
building list: Paytrace Merchant,TestMerchant
building list: Michelle's Business,Paytrace Merchant,TestMerchant
building list: Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant

  Merchants Pages of Dashboard App
    √ should verify the Merchants list view is alphabetical by Name

【问题讨论】:

  • 为什么要使用 setTimeout 在 1500 年强制退出承诺?这可能就是为什么
  • 我在其他地方看到了这个建议。在我也合并 setTimeout 之前,我得到了相同的结果。我已经用我尝试不使用 setTimeout 调用的代码更新了我的问题。

标签: arrays typescript promise protractor resolve


【解决方案1】:

你的问题是你的承诺跑掉了。在返回的承诺解决之前,您没有等待它们完成。

试试这个:

function buildMerchantListArray() {
  const list: string[] = []
  const promises: Promise<void>[] = []
  for (let i = 20; i >= 0; i--) {
    promises.push(
      this.getMerchantName(i)
        .isPresent()
        .then((present) => {
          if (present) {
            this.getMerchantName(i)
              .getText()
              .then((text: string) => {
                list.unshift(text);
                console.log('building list: ' + list)
              })
          }
        })
    )
  }
  return Promise.all(promises).then(() => list)
}

【讨论】:

  • promise.push() 中的所有内容都用红色下划线显示错误:TS2345:“promise.Promise,void>”类型的参数不可分配给“Promise”类型的参数。 'then' 的属性类型不兼容......等等。
  • promise.Promise,void&gt;??
  • 你可以把Promise&lt;void&gt;改成Promise&lt;any&gt;,但是从代码上看应该是Promise&lt;void&gt;。那应该可以解决错误。这是次要的类型检查。
  • 抱歉,错字... TS2345:“promise.Promise”类型的参数不可分配给“Promise”类型的参数。 'then' 的属性类型不兼容....等等。仍然看到同样的错误。还有一些根本性的错误。此函数仍处于错误状态。
  • 似乎您正在使用一些承诺 polyfill。改为const promises: promise.Promise&lt;void&gt;[]
猜你喜欢
  • 2019-04-29
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多