我在 2021 年遇到了同样的问题,并且能够利用@cartant 的答案,所以我想我会分享:
索引.ts
import request from 'request-promise-native';
import { from, defer } from "rxjs";
import { forkJoinConcurrent } from './forkJoinConcurrent';
const handleRequest = async (id: string) => await request.get(`http://bleh.com/${id}`, { json: true });
const ids: string[] = [...Array(10000).keys()].map((k: number) => k.toString());
const concurrent: number = 3;
/* use `defer` instead of `from` to generate the Observables.
`defer` uses a factory to generate the promise and it will execute
the factory only when it is subscribed to */
const observables = ids.map((id: string) => defer(() => from(handleRequest(id))))
forkJoinConcurrent<any>(observables, concurrent).subscribe(value => console.log(value));
forkJoinConcurrent.ts
import { from, Observable } from "rxjs";
import { last, map, mergeMap, toArray } from "rxjs/operators";
export function forkJoinConcurrent<T>(
observables: Observable<T>[],
concurrent: number
): Observable<T[]> {
// Convert the array of observables to a higher-order observable:
return from(observables).pipe(
// Merge each of the observables in the higher-order observable
// into a single stream:
mergeMap((observable, observableIndex) => observable.pipe(
// Like forkJoin, we're interested only in the last value:
last(),
// Combine the value with the index so that the stream of merged
// values - which could be in any order - can be sorted to match
// the order of the source observables:
map(value => ({ index: observableIndex, value }))
), concurrent),
// Convert the stream of last values to an array:
toArray(),
// Sort the array of value/index pairs by index - so the value
// indices correspond to the source observable indices and then
// map the pair to the value:
map(pairs => pairs.sort((l, r) => l.index - r.index).map(pair => pair.value))
);
}