Created
March 20, 2016 20:28
-
-
Save zzarcon/bb6beb8c7e3fae840482 to your computer and use it in GitHub Desktop.
xhr promises
This file contains hidden or 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
| function xhrPromise() { | |
| return new Promise(function(resolve, eject) { | |
| var xhr = new XMLHttpRequest(); | |
| xhr.onreadystatechange = onready; | |
| xhr.open("GET", "https://api.github.com/users/zzarcon"); | |
| xhr.send(); | |
| function onready() { | |
| if (xhr.readyState == 4 && xhr.status == 200) { | |
| var json = JSON.parse(xhr.responseText); | |
| resolve(json); | |
| } | |
| } | |
| }); | |
| } | |
| xhrPromise().then(callback); | |
| function callback(user) { | |
| console.log(user.name) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment