gpt4 book ai didi

javascript - 从带有 promise 的函数返回一个数组

转载 作者:行者123 更新时间:2023-11-30 11:27:30 25 4
gpt4 key购买 nike

我需要返回数组 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 promise :(property) angular.resource.IResourceArray<any>.$promise: ng.IPromise<ng.resource.IResourceArray<any>>

客户端代码(可以编辑为不期望 promise ):

$onInit() {
this.lotLogic.getLotSpecItems(this.lot.id).then((results) => {
this.searchResults = results;
console.log(this.searchResults);
});

它目前返回 undefined .我做错了什么?

最佳答案

您没有从 $promise.then() 返回任何内容。返回您的新数组,以便它在下一个 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;
});
}

关于javascript - 从带有 promise 的函数返回一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47423975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com