Created
September 16, 2012 01:13
-
-
Save mxs/3730663 to your computer and use it in GitHub Desktop.
chaining async calls with q.js
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 send = function(path) { | |
var d = Q.defer(); | |
var xhr = new XMLHttpRequest(); | |
xhr.onreadystatechange=function() | |
{ | |
if (xhr.readyState==4 && xhr.status==200) | |
{ | |
d.resolve(xhr.responseText); | |
} | |
}; | |
xhr.open("GET", serverbase + path, true); | |
log("GET ", serverbase, path); | |
xhr.send(); | |
return d.promise; | |
}; | |
log('\n[ using q.js for chaining async calls ]'); | |
// send returns a promise | |
send("scottie") | |
.then(function(data) { | |
log('received: ', data); | |
return send('michael'); | |
}) | |
.then(function(data) { | |
log('recived: ', data); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment