Wait for an async operation in onNext of RxJS Observable

前端 未结 3 961
暖寄归人
暖寄归人 2021-02-02 08:15

I have an RxJS sequence being consumed in the normal manner...

However, in the observable \'onNext\' handler, some of the operations will complete synchronously, but oth

3条回答
  •  耶瑟儿~
    2021-02-02 09:11

    Another simple example to do manual async operations.

    Be aware that it is not a good reactive practice ! If you only want to wait 1000ms, use Rx.Observable.timer or delay operator.

    someObservable.flatMap(response => {
      return Rx.Observable.create(observer => {
        setTimeout(() => {
          observer.next('the returned value')
          observer.complete()
        }, 1000)
      })
    }).subscribe()
    

    Now, replace setTimeout by your async function, like Image.onload or fileReader.onload ...

提交回复
热议问题