[英]How do you combine RxJs and NgRx to make multiple Rest-Calls and make them wait for a result?
In my Effect im listening on an Action, that value of the Action is a string Array.在我的 Effect 中,我正在监听一个 Action,该 Action 的值是一个字符串数组。 Now i need to loop over the array and use the values as inputs for the next asnyc function.
现在我需要遍历数组并将这些值用作下一个 asnyc function 的输入。
someEffect$ = createEffect(() =>
this.action$.pipe(
ofType(someAction),
mergeMap(({ someValue }) =>
this.httpCall.getSomeTh(someValue[0]).pipe(
map((result) => someOtherAction)
)
)
)
)
I have tried a few different things.我尝试了一些不同的东西。 The one below seemed to get me to the closest one i wanted.
下面的那个似乎让我找到了我想要的最接近的那个。 But since the Rest-calls need a moment to return, the array is always empty.
但是由于 Rest-calls 需要一段时间才能返回,所以数组总是空的。
someEffect$ = createEffect(() =>
this.action$.pipe(
ofType(someAction),
mergeMap(({ someValue }) => {
const array = [];
someValue.forEach((value) =>
this.http.get(value).pipe(tap((object) => array.push(object)))
)
return of(someOtherAction(array))
}
)
)
)
Does anyone have an idea, how to make the whole thing wait for the rest calls to return back?有谁知道如何让整个事情等待 rest 电话返回?
someEffect$ = createEffect(() =>
this.action$.pipe(
ofType(someAction),
mergeMap(({ someValue }) =>
forkJoin(someValue.map(value => this.http.get(value)))
)
)
)
The forkJoin
takes an array of Obserables and emits when every Observable is completed. forkJoin
接受一个 Obserables 数组,并在每个 Observable 完成时发出。 It outputs the results as an array.它将结果输出为数组。
Simply you can continue writing the next call after you finished the first one (someValue.forEach).您只需在完成第一个调用 (someValue.forEach) 后继续编写下一个调用。
The long scenario is:长期的情况是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.