Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. function get(url) {
  2. // Return a new promise.
  3. return new Promise(function(resolve, reject) {
  4. // Do the usual XHR stuff
  5. var req = new XMLHttpRequest();
  6. req.open('GET', url);
  7.  
  8. req.onload = function() {
  9. // This is called even on 404 etc
  10. // so check the status
  11. if (req.status == 200 || req.status == 0) {
  12. // Resolve the promise with the response text
  13. resolve(req.response);
  14. }
  15. else {
  16. // Otherwise reject with the status text
  17. // which will hopefully be a meaningful error
  18. console.error(req);
  19. reject(Error(req.statusText));
  20. }
  21. };
  22.  
  23. // Handle network errors
  24. req.onerror = function() {
  25. reject(Error("Network Error"));
  26. };
  27.  
  28. // Make the request
  29. req.send();
  30. });
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement