[英]javascript/Typescript return inside .then callback inside array.filter
我想過濾一個數組,但要做到這一點,我需要調用返回承諾的數據庫。 代碼:
this.arrayToFilter.filter(myObject => {
this.dataBaseService.getSomething(myObject.id).then(something => {
// some calculations
return shouldBeFiltered
})
})
如何將shouldBeFiltered
的值shouldBeFiltered
到過濾器回調中?
創建一個Promise.all
數組並在過濾之前調用Promise.all
:
const shouldBeFilteredArr = await Promise.all(
this.arrayToFilter.map(({ id }) => this.dataBaseService.getSomething(id)
);
const filteredItems = this.arrayToFilter.filter((myObject, i) => {
const something = shouldBeFilteredArr[i];
// some calculations
return shouldBeFiltered;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.