Last active
August 29, 2015 14:19
-
-
Save porfirion/e9c2b1aac6930a9437f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pr = new Promise(function(resolve, reject) { | |
var fn = function(event) { | |
console.log("Event type: %s, Event target.readyState: %d, Event target.status: %d, Event target.statusText: %s", | |
event.type, | |
event.target.readyState, | |
event.target.status, | |
event.target.statusText | |
); | |
if (event.type == "error" || event.type == "abort" || (event.type == "load" && (event.target.readyState != XMLHttpRequest.DONE || event.target.status != 200))) { | |
reject("Very bad..."); | |
} else if (event.type == "load" && event.target.readyState == XMLHttpRequest.DONE && event.target.status == "200") { | |
resolve("It's OK!"); | |
} | |
} | |
var req = new XMLHttpRequest(); | |
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequestEventTarget | |
req.addEventListener("abort", fn); | |
req.addEventListener("error", fn); | |
req.addEventListener("load", fn); | |
req.addEventListener("loadstart", fn); | |
req.addEventListener("progress", fn); | |
req.addEventListener("timeout", fn); | |
req.addEventListener("loadend", fn); | |
req.open("GET", "timeout.php"); | |
req.send(); | |
}); | |
pr.then(function() { | |
console.log("resolved", arguments); | |
}, function() { | |
console.log("rejected", arguments); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of asynchronous loading via XMLHttpRequest with Promise