Last active
August 29, 2015 14:25
-
-
Save Psvensso/96ae9a0039d50558ba9e to your computer and use it in GitHub Desktop.
RxJX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Rx = require('rx'); | |
var Promise = require('bluebird'); | |
function getPromiseByNum(num) { | |
if (num < 2) { | |
return Promise.resolve(num); | |
} else { | |
return Promise.reject(num); | |
} | |
} | |
var source = Rx.Observable.interval(1000) | |
.flatMap(function(num) { | |
return Rx.Observable.fromPromise(getPromiseByNum(num)) | |
.catch(function(e) { return Rx.Observable.just({error : e}); }); | |
}); | |
var subscription = source.subscribe( | |
function (x) { | |
if (x.error) console.log('Error: ' + x.error); | |
else console.log('Next: ' + x); | |
}, | |
function (err) { | |
console.log('Error: ' + err); | |
}, | |
function () { | |
console.log('Completed'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Rx = require('rx'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment