Skip to content

Instantly share code, notes, and snippets.

@AtsushiSuzuki
Created October 2, 2015 03:36
Show Gist options
  • Save AtsushiSuzuki/f52685142204e39ad149 to your computer and use it in GitHub Desktop.
Save AtsushiSuzuki/f52685142204e39ad149 to your computer and use it in GitHub Desktop.
Rxjsでasync.series相当の処理を行う。
const Rx = require('rx');
Rx.Observable.prototype.mapParallel = function (fn) {
return this.concatMap((v) => Rx.Observable.fromNodeCallback(fn)(v));
};
Rx.Observable.prototype.mapSeries = function (fn) {
return this.concatMap((v) => Rx.Observable.defer(() => Rx.Observable.fromNodeCallback(fn)(v)));
};
Rx.Observable.of(1, 2, 3, 4)
.mapSeries((v, done) => setTimeout(() => done(null, v), 100))
.subscribe((v) => console.log(v));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment