我需要在Angular 5中使用forkJoin并行处理请求列表。我从此链接RxJS recipes: ‘forkJoin’ with the progress of completion for bulk network requests in Angular中获取了引用>
我在这里有一个问题。我无法确切获得已完成的请求,但能够获得已完成的请求数。
任何人都可以让我知道如何知道已完成哪个特定请求吗?
这是下面的代码
let Rx = window.Rx = window['rxjs'];
let {forkJoin, Subject, merge, of, defer, concat, throwError} = Rx;
let {ajax} = Rx.ajax;
let {map, filter, tap, takeLast, scan, startWith, mergeMap, finalize, ignoreElements} = Rx.operators;
console.clear();
function forkJoinWithProgress(arrayOfObservables) {
return defer(() => {
let counter = 0;
const percent$ = new Subject();
const modilefiedObservablesList = arrayOfObservables.map(
(item, index) => item.pipe(
finalize(() => {
const percentValue = ++counter * 100 / arrayOfObservables.length;
percent$.next(percentValue);
})
)
);
const finalResult$ = forkJoin(modilefiedObservablesList).pipe(
tap(() => {
percent$.next(100);
percent$.complete();
}
));
return of([finalResult$, percent$.asObservable()]);
})
}
const getUserDetails = userIdsList => {
const arrayOfObservables = userIdsList.map((userId, index) =>{
//if (index === 1) return throwError({message: 'Vah-vah!'}); // testin with error
return ajax('https://jsonplaceholder.typicode.com/comments/' + userId)
}
)
return forkJoinWithProgress(arrayOfObservables)
}
const result$ = getUserDetails([1, 2, 15]);
result$.pipe(
mergeMap(([finalResult, progress]) => merge(
progress.pipe(
tap((value) => console.log(`${value} completed`)),
ignoreElements()
),
finalResult
))
).subscribe(values => console.log(values), console.warn);
我需要在Angular 5中使用forkJoin并行处理请求列表。我从此链接获取RxJS配方的参考:'forkJoin',了解批量网络的完成进度...