Skip to content

Instantly share code, notes, and snippets.

@benjamintanweihao
Created October 4, 2015 23:57
Show Gist options
  • Select an option

  • Save benjamintanweihao/a0527231de862ac42625 to your computer and use it in GitHub Desktop.

Select an option

Save benjamintanweihao/a0527231de862ac42625 to your computer and use it in GitHub Desktop.
function get(url) {
return Rx.Observable.create(function(observer) {
var req = new XMLHttpRequest();
req.open('GET', url);
req.onLoad = function() {
if (req.status == 200) {
observer.onNext(req.response);
observer.onCompleted();
}
else {
observer.onError(new Error(req.statusTest));
}
};
req.onError = function() {
observer.onError(Error("Unknown Error"));
};
req.send();
});
}
var test = get('https://www.wikipedia.org');
test.subscribe(
function(x) { console.log('Result: ' + x); },
function(err) { console.log('Error: ' + err); },
function() { console.log('Completed'); }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment