Skip to content

Instantly share code, notes, and snippets.

@mxs
Created September 16, 2012 01:13
Show Gist options
  • Save mxs/3730663 to your computer and use it in GitHub Desktop.
Save mxs/3730663 to your computer and use it in GitHub Desktop.
chaining async calls with q.js
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