0

I've asked "How to map a stream to lazy promise stream" with concurrency limit of 1 here: map a stream to lazy promise stream. The answer was flatMapConcat.

Now I have two nested streams, that needs concurrency limit of 1, and flatMapConcat no longer works.

var getPromise = function(value) {
  console.log('resolving ' + value);
  return new RSVP.Promise(function(resolve, reject) {
      setTimeout(function() {
        resolve(value);
      }, 1000);
  });
};


var stream = Bacon.fromArray([1,2,3]).flatMapConcat(function(value) {
  return Bacon.fromPromise(getPromise(value));
});

//stream.log();
// nice sequential
// resolving 1
// 1
// resolving 2
// 2
// resolving 3
// 3

var secondStream = stream.flatMapConcat(function(promised) {
  return Bacon.fromPromise(getPromise('second' + promised));
});


secondStream.log();
// mixed up stream and secondStream, not sequential anymore
// resolving 1
// resolving second1
// resolving 2
// second1
// resolving second2
// resolving 3
// second2
// resolving second3
// second3

// Required output:
// resolving 1
// resolving second1
// second1
// resolving 2
// resolving second2
// second2
// resolving 3
// resolving second3
// second3

The first stream is nice and sequential, but when I build a nested stream with it, flatMapConcat no longer works. I need the first stream to wait for the second stream.

Community
  • 1
  • 1
user3995789
  • 3,452
  • 1
  • 19
  • 35

1 Answers1

1

Try to move second .flatMap inside the first:

var stream = Bacon.fromArray([1,2,3]).flatMapConcat(function(value) {
  return Bacon.fromPromise(getPromise(value)).flatMapConcat(function(promised) {
    return Bacon.fromPromise(getPromise('second' + promised));
  });
});
Roman Pominov
  • 1,403
  • 1
  • 12
  • 17