How to make an RxJS subscription with an async function serialize the onNext calls?

旧时模样 提交于 2020-01-07 09:26:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!