I am using protractor to run e2e test cases. All protractor calls are async calls. The below code is a protractor code :
gridRow.then(function (rows) {
for (var index = 0; index < rows.length; index++) {
console.log(index); -------------------- 1
rows[index].all(by.className('ui-grid-cell-contents ng-scope')).get(1).getText().then(function (text) {
console.log(index); ----------------- 2
if (text.toUpperCase().replace(/ |-/g, '') === 'BRANCHONE') {
console.log(index); -------------- 3
}
});
}
});
The rows.length = 2 in this case. Console.log at 1 outputs 0 and 1 as expected but the console.log at 2 and 3 outputs as 2, because the then calls are async.Since the async calls run parallely , in the mean time the value of index gets updated to 2(index++).
Any idea about how to track that sync variale(index) inside my async calls?