How can I use RxJs to hold off any requests for an AJAX call until the previous one resolves

前端 未结 1 1073
逝去的感伤
逝去的感伤 2021-01-12 12:48

I have an observable that represents an action that is triggered by some outside component. For the purpose of this question, let\'s call it createBananaAction.

1条回答
  •  感情败类
    2021-01-12 13:20

    Just use flatMapFirst instead of flatMap:

    this.createdBananas = createBananaAction.flatMapFirst(() => bananaService.create());
    

    The above assumes your bananaService.create() returns a cold observable. If it returns a hot observable or a Promise, then we need to wrap the call with Observable.defer to convert it to a cold observable so that flatMapFirst can control its firing properly:

    this.createdBananas = createBananaAction
        .flatMapFirst(() => Rx.Observable.defer(() => bananaService.create()));
    

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