Return 来自带有承诺的函数的数组
Return an array from a function with a promise
我需要return这个函数的数组lotSpecItems
,一旦它被填充:
public getLotSpecItems(id: number) {
let toReturn = this.resourceClass.getLotSpecItems({ lotId: id });
let lotSpecItems = [];
return toReturn.$promise.then(lotSpecItemsArray => {
lotSpecItemsArray.forEach(lotSpecItem => {
lotSpecItems.push(this.$di.logicFactory.getLotSpecLogic().mapLotSpecItem(lotSpecItem));
});
});
}
$promise
是一个 angular js 承诺:(property) angular.resource.IResourceArray<any>.$promise: ng.IPromise<ng.resource.IResourceArray<any>>
客户端代码(可以编辑为不期望承诺):
$onInit() {
this.lotLogic.getLotSpecItems(this.lot.id).then((results) => {
this.searchResults = results;
console.log(this.searchResults);
});
它目前 returns undefined
。我做错了什么?
您没有从 $promise.then()
返回任何内容。 Return 您的新数组,以便在下一个 then()
中可用
public getLotSpecItems(id: number) {
let toReturn = this.resourceClass.getLotSpecItems({ lotId: id });
let lotSpecItems = [];
return toReturn.$promise.then(lotSpecItemsArray => {
lotSpecItemsArray.forEach(lotSpecItem => {
lotSpecItems.push(this.$di.logicFactory.getLotSpecLogic().mapLotSpecItem(lotSpecItem));
});
// Now return the array
return lotSpecItems;
});
}
我需要return这个函数的数组lotSpecItems
,一旦它被填充:
public getLotSpecItems(id: number) {
let toReturn = this.resourceClass.getLotSpecItems({ lotId: id });
let lotSpecItems = [];
return toReturn.$promise.then(lotSpecItemsArray => {
lotSpecItemsArray.forEach(lotSpecItem => {
lotSpecItems.push(this.$di.logicFactory.getLotSpecLogic().mapLotSpecItem(lotSpecItem));
});
});
}
$promise
是一个 angular js 承诺:(property) angular.resource.IResourceArray<any>.$promise: ng.IPromise<ng.resource.IResourceArray<any>>
客户端代码(可以编辑为不期望承诺):
$onInit() {
this.lotLogic.getLotSpecItems(this.lot.id).then((results) => {
this.searchResults = results;
console.log(this.searchResults);
});
它目前 returns undefined
。我做错了什么?
您没有从 $promise.then()
返回任何内容。 Return 您的新数组,以便在下一个 then()
public getLotSpecItems(id: number) {
let toReturn = this.resourceClass.getLotSpecItems({ lotId: id });
let lotSpecItems = [];
return toReturn.$promise.then(lotSpecItemsArray => {
lotSpecItemsArray.forEach(lotSpecItem => {
lotSpecItems.push(this.$di.logicFactory.getLotSpecLogic().mapLotSpecItem(lotSpecItem));
});
// Now return the array
return lotSpecItems;
});
}