如何引用链接函数返回的promise?

时间:2014-04-04 13:55:39

标签: javascript angularjs asynchronous promise protractor

我很擅长承诺,而且我不确定我是否正确使用它们。我试图用一些承诺进行计算,所以我将一堆函数链接在一起。首先我把它写成如下,它起作用,但看起来不是很好的代码。

it('should verify CPC column', function() {
  ptor.findElements(protractor.By.className('aggRow')).then(function(promCells) {
    getAggCellColumn('CPC').then(function(promColCPC) {
      getAggCellColumn('Spend').then(function(promColSpend) {
        getAggCellColumn('Clicks').then(function(promColClicks) {
          promCells[promColCPC].getText().then(function(promStringCPC) {
            promCells[promColSpend].getText().then(function(promStringSpend) {
              promCells[promColClicks].getText().then(function(promStringClicks) {
                var floatCPC = parseFloat(promStringCPC);
                var floatSpend = parseFloat(promStringSpend);
                var floatClicks = parseFloat(promStringClicks);
                var result = Math.round((floatSpend / floatClicks) * 100) / 100;
                expect(floatCPC).toEqual(result);
              });
            });
          });
        });
      });
    });
  });
});

我更多地研究了承诺并提出了以下建议:

it('should verify CPC column', function() {
  ptor.findElements(protractor.By.className('aggRow'))
    .then(getAggCellColumn('CPC')) 
    .then(getAggCellColumn('Spend')) 
    .then(getAggCellColumn('Clicks')) 
    .then(promCells[promColCPC + 2].getText()) 
    .then(promCells[promColSpend + 2].getText()) 
    .then(promCells[promColClicks + 2].getText()) 
    .then(function(promStringClicks) {
      var floatCPC = parseFloat(promStringCPC);
      var floatSpend = parseFloat(promStringSpend);
      var floatClicks = parseFloat(promStringClicks);
      var result = Math.round((floatSpend / floatClicks) * 100) / 100;
      expect(floatCPC).toEqual(result);
    });
});

这不起作用,但我试图将findElements的结果作为promCells。我还想引用getText()函数的结果。如果我应该以不同的方式处理这个问题,请告诉我。谢谢!

我也在想这样写......

it('should verify CPC column', function() {
  ptor.findElements(protractor.By.className('aggRow'))
    .then(function(promStringClicks) {
      var floatCPC = parseFloat(promCells[getAggCellColumn('CPC') + 2].getText());
      var floatSpend = parseFloat(promCells[getAggCellColumn('Spend') + 2].getText());
      var floatClicks = parseFloat(promCells[getAggCellColumn('Clicks') + 2].getText());
      var result = Math.round((floatSpend / floatClicks) * 100) / 100;
      expect(floatCPC).toEqual(result);
  });
});

1 个答案:

答案 0 :(得分:1)

JS中的Promises概念是函数链,具有错误处理功能。在您的情况下,您只能使用链接。

因此,为了锁定您的电话,您必须传递函数,这可以返回一些值供链的下一个函数使用。

请注意,在单元测试中,it必须知道您的链何时结束,因此您可以期待done函数并在链完成后调用它。

你最终会有类似的东西:

it('should verify CPC column', function(done) {
    var promStringCPC, promStringSpend, promStringClicks;
    ptor.findElements(protractor.By.className('aggRow'))
        .then(function (promCells) {
            // Do something from async result like the following...
            promStringCPC = promCells.getAggCellColumn('CPC');
            promStringSpend = promCells.getAggCellColumn('Spend');
            promStringClicks = promCells.getAggCellColumn('Clicks');

            var floatCPC = parseFloat(promStringCPC);
            var floatSpend = parseFloat(promStringSpend);
            var floatClicks = parseFloat(promStringClicks);
            var result = Math.round((floatSpend / floatClicks) * 100) / 100;
            expect(floatCPC).toEqual(result);

            // You can do async additional stuff here...
            // return asyncResultOrNestedPromise
        }) 
        .then(function (result) {
            // ... and use the async's result here
        })
        .then(done);
});

请注意,我没有清楚地了解您的具体情况。无论如何,我试图让你理解承诺的概念!