本文介紹了量角器 - 如何將一組承諾的結(jié)果放入另一個(gè)數(shù)組的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我從這段代碼中得到了一系列承諾:element.all(by.repeater('unit in units'))
,我發(fā)現(xiàn)很難將數(shù)據(jù)放入另一個(gè)數(shù)組:
I got an array of promises from this code: element.all(by.repeater('unit in units'))
, and I am finding it really difficult to get the data into another array:
element.all(by.repeater('unit in units')).then(function (arr) {
var items = [];
for (var i = 0; i < arr.length; i++) {
arr[i].getText().then(function(text) {
items.push(text);
});
}
//PROBLEM ITEMS is Empty
console.log(items);
});
推薦答案
設(shè)法以更簡單的方式獲得相同的結(jié)果,避免使用 Q 和中繼器.使用內(nèi)置地圖就可以了.
Managed to get the same result on a simpler way avoiding using Q and the repeater. Using the inbuilt map does the trick.
var tabs = element.all(by.css('.unitTabs li a')).map(function (elm) {
return elm.getText();
});
tabs.then(function (result) {
var sorted = _.sortBy(result, function (name) { return name; });
for (var i = 0; i < result.length; i++) {
expect(result[i]).toBe(sorted[i]);
}
});
這篇關(guān)于量角器 - 如何將一組承諾的結(jié)果放入另一個(gè)數(shù)組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!