turtlepower turtlepower - 6 months ago 19
Node.js Question

Using the Request callback in NPM to call additional functions

I was wondering if it's possible to have the request package to perform callbacks after call backs.

Below is an example of what I'm trying to achieve:

getToken().then(function(accessToken) {
var deferred = q.defer();
try{
var accessToken = accessToken
console.log('The token is: '+ accessToken)
return deferred.promise;
}catch(error){
deferred.reject(error);
console.log(error)
}
}.then(function facebookRequest(token))
)

Answer

You should use request-promise, its made exactly for this: https://www.npmjs.com/package/request-promise

Example:

requestPromise('http://www.google.com')
    .then(function (htmlString) {
        // Process html... 
    })
    .catch(function (err) {
        // Crawling failed... 
    });