luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
531 stars 29 forks source link

How to use waitfor with request #7

Closed JBX028 closed 11 years ago

JBX028 commented 11 years ago

Hi,

How to use waitfor with the request module (https://github.com/mikeal/request):

    request.get({
            uri: 'https:blabla',
            headers: {'Authorization': 'Bearer ' + _BEARER}},
            function (error, response, body) {
                if(error) {
                    console.log(error);
                } else {
                    output = JSON.parse(body)
                    return output.outcome.intent;
                }
            }
        );

How can I pass what is submitted to uri and header ? I tried this but without success...:

var myRequest = wait.forMethod(request, "get", {
            uri: 'https:blabla',
            headers: {'Authorization': 'Bearer ' + _BEARER}})

Thanks in advance for the help you can provide.

luciotato commented 11 years ago

You're using the parameters correctly, the problem is Wait.for is expecting standardized async with callback(err,data) as the last parameter, sou you must wrap request.get in order to standardize it:

//request.get standardized
request.standardGet = function ( options, callback) {
    request.get(options,
            function (error, response, body) {
                //standardized callback
                var data={ response: response, body:body};
                callback(error,data);
            });
}

...
// in a fiber
...
var data = wait.for( request.standardGet, 
     { uri: 'https:blabla'
       ,headers: {'Authorization': 'Bearer ' + _BEARER}
     });
output = JSON.parse(data.body)
return output.outcome.intent;

Note: Just writing it here - I did not try this code.

JBX028 commented 11 years ago

ok thanks ! :+1: