Return for 循环内的承诺
Return promise inside the for loop
我有如下逻辑,
getSpecificCell: function(tableObject, rowText, columnCss) {
var ele = element.all(by.repeater(tableObject)).count().then(function(count) {
for (var i = 0; i < count; i++) {
return element(by.repeater(tableObject).row(i)).getText().then(function(txt) {
if (txt.indexOf(rowText) !== -1) {
return element(by.repeater(tableObject).row(i)).element(by.css('[' + columnCss + ']'));
}
});
}
});
return ele;
}
但它 return 是第一次迭代本身的值。
return 这种 for 循环中的 promise 是否可行,或者我们对此有任何其他解决方案吗?
首先,您不需要对 ElementArrayFinder 使用 for
循环。这就是 each() 方法的用途。
其次,您根本不需要循环。听起来您应该使用 filter() 来获取符合您的规范的 table 单元格,但我不确定您到底想要完成什么。
var table = element.all(by.repeater(tableObject));
// list is an ElementArrayFinder of all elements that matched the filter
var list = table.filter(function (elem) {
return elem.getText().then(function (text) {
return txt.indexOf(rowText) !== -1
})
});
// do something with list
list.count().then(function (count) {
console.log(count);
});
我有如下逻辑,
getSpecificCell: function(tableObject, rowText, columnCss) {
var ele = element.all(by.repeater(tableObject)).count().then(function(count) {
for (var i = 0; i < count; i++) {
return element(by.repeater(tableObject).row(i)).getText().then(function(txt) {
if (txt.indexOf(rowText) !== -1) {
return element(by.repeater(tableObject).row(i)).element(by.css('[' + columnCss + ']'));
}
});
}
});
return ele;
}
但它 return 是第一次迭代本身的值。 return 这种 for 循环中的 promise 是否可行,或者我们对此有任何其他解决方案吗?
首先,您不需要对 ElementArrayFinder 使用 for
循环。这就是 each() 方法的用途。
其次,您根本不需要循环。听起来您应该使用 filter() 来获取符合您的规范的 table 单元格,但我不确定您到底想要完成什么。
var table = element.all(by.repeater(tableObject));
// list is an ElementArrayFinder of all elements that matched the filter
var list = table.filter(function (elem) {
return elem.getText().then(function (text) {
return txt.indexOf(rowText) !== -1
})
});
// do something with list
list.count().then(function (count) {
console.log(count);
});