How Do I Reference A Promise Returned From A Chained Function?
I'm new to promises, and I'm not sure if I'm using them correctly. I'm trying to make a calculation with some promises, so I chained a bunch of functions together. First I wrote i
Solution 1:
Promises concept in JS is function chaining, with error handling. In your case, you're okay with chaining only.
So in order to chain your calls, you must pass functions, that can return some value to be used by the next function of the chain.
Note that in unit testing, the it
s have to know when your chain ends, so you can expect the done
function and call it when your chain has finished.
You finally would have something like:
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);
});
Note that I didn't clearly get your specific case. Anyway I'm trying to make you understand the promises concept!
Post a Comment for "How Do I Reference A Promise Returned From A Chained Function?"