我如何 return 在 then 语句中的一系列承诺
How can I return an array of promises in a then statement
所以在过去的几个小时里,我一直在研究异步的东西并使用 promises。我正在使用测试框架量角器,我遇到了一些异步问题。
在这个保存函数中,我异步调用了cm.org1.all(),然后使用then来获取响应。我遍历响应,我需要调用 getNewElement() 到响应中的每个元素,其中也有一个异步调用,所以每个 return 都是一个承诺。
所以我有这一系列承诺,但我不知道如何return。 cm.save()的return是[]。我需要它是 ['foo',foo',foo',foo']
下面的这段代码不起作用,但这是我目前所拥有的。
var cm = companyManagement() {
//whatever is initialized below is initialized up here
this.save = function() {
cm.saveButton.click();
var elements;
var promises = [];
var defer = protractor.promise.defer();
cm.org1.all(by.repeater('brand in vm.brands'))
.then(function(response) {
//elements is of length 4
elements = response;
for(var i=0; i<elements.length;i++) {
promises.push(getNewElement(elements, i));
}
//defer.fulfill(promises); not correct?
});
return protractor.promise.all(promises); //not correct?
};
function getNewElement(elements, i) {
var defer = protractor.promise.defer();
var alias = elements[i].element(by.binding('brand.alias'));
alias.getText().then(function(aliasText) {
defer.fulfill('foo');
});
return defer.promise;
}
}
cm.save()
.then(function(response){
console.log("my new array is",response);
});
在测试中手动处理量角器承诺通常表明问题过于复杂。 Protractor
拥有涵盖大多数用例的各种抽象和函数式编程工具。
如果你使用中继器的.column()
:
,你可以在一行中解决它
this.save = function() {
cm.saveButton.click();
return cm.org1.all(by.repeater('brand in vm.brands').column('brand.alias')).getText();
};
I would do this, but I want to eventually be able to get the element's parent, if the column brand.alias matches with a certain constant I have. if I only have the alias text I couldn't be able to get the parent, correct me if I'm wrong.
cm.org1.all(by.repeater('brand in vm.brands').column('brand.alias')).filter(alias) {
return alias.getText().then(function (aliasText) {
return aliasText === "foo";
});
}).map(function (alias) {
return alias.element(by.xpath("..")).getText(); // getting the parent
});
尝试:
this.save = function() {
cm.saveButton.click();
var elements;
var promises = [];
var defer = protractor.promise.defer();
return cm.org1.all(by.repeater('brand in vm.brands'))
.then(function(response) {
//elements is of length 4
elements = response;
for(var i=0; i<elements.length;i++) {
promises.push(getNewElement(elements, i));
}
return protractor.promise.all(promises);
});
};
所以在过去的几个小时里,我一直在研究异步的东西并使用 promises。我正在使用测试框架量角器,我遇到了一些异步问题。
在这个保存函数中,我异步调用了cm.org1.all(),然后使用then来获取响应。我遍历响应,我需要调用 getNewElement() 到响应中的每个元素,其中也有一个异步调用,所以每个 return 都是一个承诺。
所以我有这一系列承诺,但我不知道如何return。 cm.save()的return是[]。我需要它是 ['foo',foo',foo',foo']
下面的这段代码不起作用,但这是我目前所拥有的。
var cm = companyManagement() {
//whatever is initialized below is initialized up here
this.save = function() {
cm.saveButton.click();
var elements;
var promises = [];
var defer = protractor.promise.defer();
cm.org1.all(by.repeater('brand in vm.brands'))
.then(function(response) {
//elements is of length 4
elements = response;
for(var i=0; i<elements.length;i++) {
promises.push(getNewElement(elements, i));
}
//defer.fulfill(promises); not correct?
});
return protractor.promise.all(promises); //not correct?
};
function getNewElement(elements, i) {
var defer = protractor.promise.defer();
var alias = elements[i].element(by.binding('brand.alias'));
alias.getText().then(function(aliasText) {
defer.fulfill('foo');
});
return defer.promise;
}
}
cm.save()
.then(function(response){
console.log("my new array is",response);
});
在测试中手动处理量角器承诺通常表明问题过于复杂。 Protractor
拥有涵盖大多数用例的各种抽象和函数式编程工具。
如果你使用中继器的.column()
:
this.save = function() {
cm.saveButton.click();
return cm.org1.all(by.repeater('brand in vm.brands').column('brand.alias')).getText();
};
I would do this, but I want to eventually be able to get the element's parent, if the column brand.alias matches with a certain constant I have. if I only have the alias text I couldn't be able to get the parent, correct me if I'm wrong.
cm.org1.all(by.repeater('brand in vm.brands').column('brand.alias')).filter(alias) {
return alias.getText().then(function (aliasText) {
return aliasText === "foo";
});
}).map(function (alias) {
return alias.element(by.xpath("..")).getText(); // getting the parent
});
尝试:
this.save = function() {
cm.saveButton.click();
var elements;
var promises = [];
var defer = protractor.promise.defer();
return cm.org1.all(by.repeater('brand in vm.brands'))
.then(function(response) {
//elements is of length 4
elements = response;
for(var i=0; i<elements.length;i++) {
promises.push(getNewElement(elements, i));
}
return protractor.promise.all(promises);
});
};