0

I use Rx.js and JqueryPromises I have run into a situation where i call a method to process an event published by Rx. This method however returns a promise.

eventStream.Subscribe(function(e) { methodThatReturnsPromise(e);});

The problem is that the processing of events is no longer deterministic. As methodThatReturnsPromise returns immediately as it defers processing.

My question :) Is there a way to "wait" on a promise? Is there any hooks for Rx to use promises i.e. chain the returned promise of a subscribe to the next returned promise of subscribe, so messages are still processed in order?

Adam Mills
  • 7,719
  • 3
  • 31
  • 47

1 Answers1

0

This code seems to do the trick for anyone else interested. I just need to chain the return deferreds.

Fiddle here

var seed = $.Deferred();
seed.resolve();
observable.aggregate(seed, function(chain, n){
    return chain.then(function(){
        console.log("Starting on Event " + n);
        return process(n);
    });
}).subscribe();
Adam Mills
  • 7,719
  • 3
  • 31
  • 47
  • If Rx has methods that return deferrds/promises then it must either have its own methods for handling done/fail/progress, or it is compatible with jQuery/q (or similar) and jQuery/q methods can be applied. The code above may well work but it's hard to see how `seed` makes a contribution, given that it is resolved as soon as it is created. – Beetroot-Beetroot Mar 29 '13 at 22:57
  • Seed is just the first value of chain (otherwise it would be null, cleaner than null checks to me). This needs to be modified to allow continution of that chain on errors as well. – Adam Mills Mar 30 '13 at 00:26
  • Then it's hard to see why you need a chain. Can you not handle the promise of interest without building a chain, the first element of which is guaranteed to be resolved? Chaining seems to be an unnecessary elaboration. – Beetroot-Beetroot Mar 30 '13 at 00:30
  • 1
    observable is a stream of "somethings", each something needs to be processed before the next something is processed (observable is a never ending stream of somethings). However the processor method used promises so it is deferred, control immediately returns to the stream (which may have another something waiting) which then would start processing another something if we don't sychronise the processing. Hence the chaining. – Adam Mills Mar 30 '13 at 00:40
  • Adam, OK that makes a whole lot more sense now. – Beetroot-Beetroot Mar 30 '13 at 00:41