Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active September 21, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/3aac1b730dbfe2a0ac5c to your computer and use it in GitHub Desktop.
Save mattpodwysocki/3aac1b730dbfe2a0ac5c to your computer and use it in GitHub Desktop.
How do I ensure that when someone throws from inside a subscribe call that it is honored and not swallowed by the Promise implementation? How do I break free from the jail?
var FromPromiseObservable = (function(__super__) {
inherits(FromPromiseObservable, __super__);
function FromPromiseObservable(p) {
this.p = p;
__super__.call(this);
}
FromPromiseObservable.prototype.subscribeCore = function(o) {
this.p
.then(function (data) {
o.onNext(data);
o.onCompleted();
}, function (err) {
o.onError(err);
});
return disposableEmpty;
};
return FromPromiseObservable;
}(ObservableBase));
/**
* Converts a Promise to an Observable sequence
* @param {Promise} An ES6 Compliant promise.
* @returns {Observable} An Observable sequence which wraps the existing promise success and failure.
*/
var observableFromPromise = Observable.fromPromise = function (promise) {
return new FromPromiseObservable(promise);
};
var Rx = require('rx');
var disp = Rx.Observable.fromPromise(Promise.resolve(42))
.subscribe(function (x) {
// This somehow gets caught by Node.js and never seen
throw new Error();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment