Skip to content

Instantly share code, notes, and snippets.

@Pierozi
Created April 6, 2017 08:02
Show Gist options
  • Select an option

  • Save Pierozi/1e90a39d059b73471dc10bf7e7af21fd to your computer and use it in GitHub Desktop.

Select an option

Save Pierozi/1e90a39d059b73471dc10bf7e7af21fd to your computer and use it in GitHub Desktop.
RxJs Operator multiplyBy
// Credit andre-staltz
// from : https://egghead.io/instructors/andre-staltz
const data = [1,2,3,4,5];
var foo = Rx.Observable.of(...data);
// foo.map
// foo.filter
// foo.merge
// foo.combineLatest
function multiplyBy(multiplier) {
var source = this;
var result = Rx.Observable.create(function subscribe(observer) {
source.subscribe(
function (x) { observer.next(x * multiplier); },
function (err) { observer.error(err); },
function () { observer.complete(); }
);
});
return result;
}
Rx.Observable.prototype.multiplyBy = multiplyBy;
var bar = foo.multiplyBy(100);
bar.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment