问题
This is a followup for this question.
So RxJS is calling my async function with the next event bebore the previous one has completed as soon as I await in my async method.
I need to serialize the calls to this async function.
I have understood from this answer to a similar question that I need to move my async function away from subscribe and use concatMap.
Right now my code does not compile with the following error:
error TS2339: Property 'concatMap' does not exist on type 'Observable'.
My code (trying to adjust):
1/ the new subscription code (won't compile):
this.emitter = fromEventPattern(this.addHandler, this.removeHandler, (err, char) => [err, char]); <= unchanged
this.rxSubscription = this.rxSubscription = this.emitter.concatMap(value:any => this.handleUpdatedValuesComingFromSensor(value)).subscribe(); <= concatMap does not exist on type Observable<any>
2/ the async function for your information:
handleUpdatedValuesComingFromSensor = async (arr: any[]): Promise<void> => {
...
await someMethodAsync();
...
}
concatMap should be used on another type of source but I can't figure it out.
Thanks in advance.
回答1:
As Kos stated, in Rxjs v6, pipeable operators became the norm, moving away from chaining everything together with .
.
I assume since you're using fromEventPattern
, instead of Observable.fromEventPattern
that you are using rxjs v6+, in which case, you need to wrap concatMap()
inside a pipe()
.
this.rxSubscription = this.emitter.pipe(concatMap(value:any => this.handleUpdatedValuesComingFromSensor(value))).subscribe()
来源:https://stackoverflow.com/questions/55835700/how-to-make-an-rxjs-subscription-with-an-async-function-serialize-the-onnext-cal