How can I `await` on an Rx Observable?

前端 未结 5 730
孤城傲影
孤城傲影 2020-11-28 04:39

I\'d like to be able to await on an observable, e.g.

const source = Rx.Observable.create(/* ... */)
//...
await source;

A naive attempt res

相关标签:
5条回答
  • 2020-11-28 05:16

    If toPromise is deprecated for you, you can use .pipe(take(1)).toPromise but as you can see here it's not deprecated.

    So please juste use toPromise (RxJs 6) as said:

    //return basic observable
    const sample = val => Rx.Observable.of(val).delay(5000);
    //convert basic observable to promise
    const example = sample('First Example')
      .toPromise()
      //output: 'First Example'
      .then(result => {
        console.log('From Promise:', result);
      });
    

    async/await example:

    //return basic observable
    const sample = val => Rx.Observable.of(val).delay(5000);
    //convert basic observable to promise
    const example = await sample('First Example').toPromise()
    // output: 'First Example'
    console.log('From Promise:', result);
    

    Read more here.

    And please remove this wrong claim saying toPromise is deprecated.

    0 讨论(0)
  • 2020-11-28 05:18

    It likely has to be

    await observable.first().toPromise();
    

    As it was noted in comments before, there is substantial difference between take(1) and first() operators when there is empty completed observable.

    Observable.empty().first().toPromise() will result in rejection with EmptyError that can be handled accordingly, because there really was no value.

    And Observable.empty().take(1).toPromise() will result in resolution with undefined value.

    0 讨论(0)
  • 2020-11-28 05:20

    You have to pass a promise to await. Convert the observable's next event to a promise and await that.

    if (condition) {
      await observable.first().toPromise();
    }
    

    Edit note: This answer originally used .take(1) but was changed to use .first() which avoids the issue of the Promise never resolving if the stream ends before a value comes through.

    0 讨论(0)
  • 2020-11-28 05:25

    You will need to await a promise, so you will want to use toPromise(). See this for more details on toPromise().

    0 讨论(0)
  • 2020-11-28 05:27

    Use the new firstValueFrom() or lastValueFrom() instead of toPromise(), which as pointed out here, is deprecated starting in RxJS 7, and will be removed in RxJS 8.

    import { firstValueFrom} from 'rxjs';
    import { lastValueFrom } from 'rxjs';
    
    this.myProp = await firstValueFrom(myObservable$);
    this.myProp = await lastValueFrom(myObservable$);
    

    This is available in RxJS 7+

    See: https://indepth.dev/rxjs-heads-up-topromise-is-being-deprecated/

    0 讨论(0)
提交回复
热议问题