【发布时间】:2020-01-11 12:30:03
【问题描述】:
我有一个对象数组。我必须通过 API 调用检查每个对象,以查看该对象是否有资格获得某个促销。 在调用最后一个对象(其中 observable 应返回 false 或其中一个对象从 API 获得真实状态)之前,继续使用对象调用 API 的最佳方法是什么?
目前我将其作为代码使用,但感觉应该有更好的 RxJS 运算符方法。
checkProductForPromo(items: []) {
const promoChecker$ = new EventEmitter<boolean>();
items.forEach((item, key, arr) => {
// This does a HTTP callback to the API
this.ApiService.getProductInformation(item).subscribe(
(product) => {
// Based on some properties this will return true or false of the product is eligible for the promo.
if (product.isEligibleForPromo()) {
promoChecker$.emit(true);
} else if (Object.is(arr.length - 1, key)) {
promoChecker$.emit(false);
}
}
);
});
return promoChecker$;
}
【问题讨论】:
标签: typescript rxjs rxjs-pipeable-operators