Skip to content

Instantly share code, notes, and snippets.

@cdimascio
Last active January 6, 2016 18:50
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 cdimascio/9ea78af75d92b3d599d3 to your computer and use it in GitHub Desktop.
Save cdimascio/9ea78af75d92b3d599d3 to your computer and use it in GitHub Desktop.
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