你可以使用
mergeMap
带油门。代码如下所示:
Observable
// From takes an array and breaks each item into an individual event
.from(arrayOfItems)
// A mergeMap takes an event and converts it into a new observable
// in this case -- a request. The second argument is how many items to
// allow in flight at once -- you can limit your network activity here.
.mergeMap(item => saveItemObservable(item), requestLimit)
// Will emit an event once all of the above requests have completed
.toArray()
// You will have an array of response results.
.subscribe(arrayOfResults => { ... });
如果您使用的是较新版本的RxJS,则如下所示:
// You will need the correct imports:
// i.e import { from } from 'rxjs/observable/from';
// i.e. import { mergeMap, toArray } from 'rxjs/operators';
// From takes an array and breaks each item into an individual event
from(arrayOfItems).pipe(
// A mergeMap takes an event and converts it into a new observable
// in this case -- a request. The second argument is how many items to
// allow in flight at once -- you can limit your network activity here.
mergeMap(item => saveItemObservable(item), requestLimit),
// Will emit an event once all of the above requests have completed
toArray()
)
// You will have an array of response results.
.subscribe(arrayOfResults => { ... });
如果删除
toArray
对于每个已完成的事件,您将在订阅中获得一个事件。