Skip to content

Instantly share code, notes, and snippets.

@sdeering
Created April 21, 2014 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdeering/11131879 to your computer and use it in GitHub Desktop.
Save sdeering/11131879 to your computer and use it in GitHub Desktop.
node.js xhr with promises using q library
function _functionName(url) {
return Q.fcall(function () {
var deferred = Q.defer();
var options = {
url: url,
headers: {
'User-Agent': 'my-app'
},
json: true,
timeout: 60000
};
console.log( options.url );
request.get( options, function ( err, res, body ) {
console.log(body);
deferred.resolve( body );
});
return deferred.promise;
}).then( function( results ) {
console.log('parse the results...');
}).then( function ( results ) {
return Q.all( results );
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment