Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxJs: Adapt jQuery.getJSON to an Rx.Observable
function getJSON(url, data) {
return Rx.Observable.create(observer => {
var canceled = false;
if (!canceled) {
jQuery.getJSON(url, data).
done(data => {
observer.onNext(data);
observer.onCompleted();
}).
fail(err => observer.onError(err));
}
return () => canceled = true;
});
}
getJSON('https://api.ipify.org', { format: 'json' }).forEach(e => console.log(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment