Skip to content

Instantly share code, notes, and snippets.

@jhusain
Last active August 29, 2015 14:26
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 jhusain/3c510869ec091772c440 to your computer and use it in GitHub Desktop.
Save jhusain/3c510869ec091772c440 to your computer and use it in GitHub Desktop.
Observable map function
class Observable {
map(fn) {
if (typeof fn !== "function")
throw new TypeError(fn + " is not a function");
let C = this.constructor[Symbol.species];
return new C(observer => {
this[Symbol.observer]({
start(subscription) {
this.subscription = subscription;
observer.start(subscription);
},
next(value) {
try { value = fn(value) }
catch (e) {
this.subscription.dispose();
return observer.error(e)
}
return observer.next(value);
},
error(value) { return observer.error(value) },
complete(value) { return observer.complete(value) },
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment