RxJS sequence equivalent to promise.then()?

前端 未结 8 1392
南旧
南旧 2020-11-28 05:12

I used to develop a lot with promise and now I am moving to RxJS. The doc of RxJS doesn\'t provide a very clear example on how to move from promise chain to observer sequenc

8条回答
  •  执念已碎
    2020-11-28 05:27

    if getPromise function is in a middle of a stream pipe you should simple wrap it into one of functions mergeMap, switchMap or concatMap (usually mergeMap):

    stream$.pipe(
       mergeMap(data => getPromise(data)),
       filter(...),
       map(...)
     ).subscribe(...);
    

    if you want to start your stream with getPromise() then wrap it into from function:

    import {from} from 'rxjs';
    
    from(getPromise()).pipe(
       filter(...)
       map(...)
    ).subscribe(...);
    

提交回复
热议问题