Rx.js wait for callback to complete

前端 未结 1 2044
盖世英雄少女心
盖世英雄少女心 2020-12-11 11:43

I am using Rx.js to process the contents of a file, make an http request for each line and then aggregate the results. However the source file contains thousands of lines an

相关标签:
1条回答
  • 2020-12-11 12:12

    The problem here is probably that when you use rx.Observable.fromCallback, the function you passed in argument is executed immediately. The observable returned will hold the value passed to the callback at a later point in time. To have a better view of what is happening, you should use a slightly more complex simulation : number your requests, have them return an actual (different for each request) result that you can observe through the subscription.

    What I posit happens here :

    • take(5) issues 5 values
    • map issues 5 log messages, executes 5 functions and passes on 5 observables
    • those 5 observables are handled by concatAll and the values issued by those observables will be in order as expected. What you are ordering here is the result of the call to the functions, not the calls to the functions themselves.

    To achieve your aim, you need to call your observable factory (rx.Observable.fromCallback) only when concatAll subscribes to it and not at creation time. For that you can use defer : https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/defer.md

    So your code would turn into :

    rxNode.fromReadableStream(fs.createReadStream('./path-to-file'))
      .map(t => t.toString().split('\r\n'))
      .flatMap(t => t)
      .take(5)
      .map(t => {
        console.log('Submitting request');
    
        return Observable.defer(function(){return doHttpRequest(t);})
      })
      .concatAll()
      .subscribe(results => {
        console.log(results);
      }, err => {
        console.error('Error', err);
      }, () => {
        console.log('Completed');
      });
    

    You can see a similar issue with an excellent explanation here : How to start second observable *only* after first is *completely* done in rxjs

    Your log is likely to still show 5 consecutive 'Submitting request' messages. But your request should be executed one after the other has completed as you wish.

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