为什么我的方法在返回之前不等待承诺解决?

时间:2018-01-07 22:24:06

标签: arrays typescript promise protractor resolve

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

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

正如您在下面所看到的,即使我已经构建了一个promise并且正在调用.then()来返回响应,我尝试输出最终列表的各种方式也会在列表构建之前返回我的it()声明。

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

1 个答案:

答案 0 :(得分:0)

你遇到的问题是你的承诺会消失。在返回的承诺结算之前,你没有等待它们完成。

试试这个:

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)
}