Simon Boudrias
Simon Boudrias

Reputation: 44589

Why are Observable operations called once (duplicated) for every subscriber?

Each time I call .subscribe() on an Observable, the processing on each value is restarted (in the example below, the map function will be called twice for each value).

var rx = require('rx-lite');
var _ = require('lodash');

var obs = rx.Observable.fromArray([1, 2]);

var processing = obs.map(function (number) {
  // This function is called twice
  console.log('call for ' + number);
  return number + 1;
});

processing.subscribe(_.noop, _.noop);
processing.subscribe(_.noop, _.noop);

Is there any way to have subscribe give you the processed value without rerunning the whole processing functions?

Upvotes: 5

Views: 590

Answers (0)

Related Questions