Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get(url) {
- // Return a new promise.
- return new Promise(function(resolve, reject) {
- // Do the usual XHR stuff
- var req = new XMLHttpRequest();
- req.open('GET', url);
- req.onload = function() {
- // This is called even on 404 etc
- // so check the status
- if (req.status == 200 || req.status == 0) {
- // Resolve the promise with the response text
- resolve(req.response);
- }
- else {
- // Otherwise reject with the status text
- // which will hopefully be a meaningful error
- console.error(req);
- reject(Error(req.statusText));
- }
- };
- // Handle network errors
- req.onerror = function() {
- reject(Error("Network Error"));
- };
- // Make the request
- req.send();
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement