I'm trying to make a chain of promises functions which use HTTP requests in NodeJS with Kraken framework.
My code could work in 90% of cases, but if the distant requested server takes time to respond, the code will return an error with undefined values. So I think Q is a good solution to prevent that.
Here's the situation :
We access to a URL with a "code" parameter -> the route controller takes this param to use it in a HTTP POST request -> the response (a token) is stored in a variable and used in an other HTTP GET request -> the response (multiple JSON objects) is stored in variable too -> all variables are stored in a MongoDB. If functions are not used in this order, of course it fails.
var Q = require('q');
module.exports = function (router) {
  router.get('/', function (req, res) {
    var codein = req.param('code');
    if(codein){
        console.log('Provided code: ' + codein+'\n');
        getAccessToken(codein).then(function(token){
            console.log('Provided AccessToken: ' + token + '\n');
            getUsername(token).then(function(userdata){
                console.log('Provided Username: ' + JSON.parse(userdata).username + '\n');
                storeData(userdata).then(function(msg){
                    console.log(msg);
                    res.redirect('/dashboard/' + JSON.parse(userdata).username);
                });
            });
        });
    }
    else{
        console.log('Access Denied, redirecting...');
        res.redirect('/');
    }
  });
};
This method works, but actually didn't resolve the problem, because sometimes variable are undefined again. I think it's my request functions which aren't well made...
Here's an example of the first function with POST request :
var getAccessToken = function(cod){
    var def = Q.defer();
    var data = querystring.stringify({
            client_id:"1234567890", 
            client_secret:"******", 
            grant_type:"authorization_code", 
            redirect_uri:"http://localhost:8000/r/callback", 
            code:cod
    });
    var options = {
        host: 'domain.server.com',
        port: 443,
        path: '/api/oauth2/token',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': Buffer.byteLength(data)
        }
    };
    var response = "";
    var req = https.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            response += chunk;
        });
        res.on('end', function(){
            var json = JSON.parse(response);
            var acto = json.access_token;
            def.resolve(acto);
        });
    });
    req.write(data);
    req.end();
    return def.promise;
};
In this case the acto variable can be undefined... So am I using Q in a wrong way ?
EDIT
To understand my problem, let me show you what can I have in my output console (really rare but happens) :
Provided code: 12345678910
Provided Username: user543210
Instead of :
Provided code: 12345678910
Provided AccessToken: 9876543210
Provided Username: user
 
    