Ice Man Ice Man - 6 months ago 7
Javascript Question

NodeJS Promise never reaches third 'then()' call

I have a promise that I made to handle logging into one of my websites, then it is supposed to gather some information.

My Code:

var Promise = require('promise');
function login(user, pass){
return new Promise(function (fulfill, reject){
var options = {
url: 'https://url.com/this',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/page'
}
};
request(options, function(err,httpResponse,body){
fulfill(body);
});
}).then(function(html){
var parsed = parseForm(html);
parsed['post_user_name'] = user;
parsed['post_user_pass'] = pass;
return parsed;
}).then(function(cred){
var query = querystring.stringify(cred);
var options = {
url: 'https://url.com/next',
method: 'POST',
form: query,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/this'
}
};
request(options, function(err,httpResponse,body){
return 'gonext';
});
}).then(function(success){
query = querystring.stringify(cred);
options = {
url: 'https://url.com/loggedin',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/loggedin'
}
};
request(options, function(err,httpResponse,body){
return 'go again';
});
}).then(function(success){
console.log('here');
query = querystring.stringify(cred);
options = {
url: 'https://url.com/myaccount',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://google.com/account'
}
};
request(options, function(err,httpResponse,body){
fs.writeFile('text.txt', body);
return parseAuths(body);
});
});
}


My problem: the promise never reaches the third
then()
function.

Similarly, if I write my code with all the request calls in a single
then()
then try to pass the data I need, to the next then, it doesn't reach there either.

function login(user, pass){
return new Promise(function (fulfill, reject){
var options = {
url: 'https://url.com/this',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/'
}
};
request(options, function(err,httpResponse,body){
fulfill(body);
});
}).then(function(html){
var parsed = parseForm(html);
parsed['post_user_name'] = user;
parsed['post_user_pass'] = pass;
return parsed;
}).then(function(cred){
var query = querystring.stringify(cred);
var options = {
url: 'https://url.com/that',
method: 'POST',
form: query,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/this'
}
};
request(options, function(err,httpResponse,body){
query = querystring.stringify(cred);
options = {
url: 'https://url.com/another',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/that'
}
};
request(options, function(err,httpResponse,body){
query = querystring.stringify(cred);
options = {
url: 'https://url.com/account',
method: 'GET',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Referer': 'https://url.com/another'
}
};
request(options, function(err,httpResponse,body){
fs.writeFile('text.txt', body);
return parseAuths(body);
});
});
});
}).then(function(data){
console.log(data); // Never reaches this
});
}


My question: am I doing anything blatantly wrong? Is there something I am missing?

Answer

This approach makes use of request-promise. You can make all of your requests as promises and keep the chain going as such. If you're experiencing an error it will properly be propagated to the nearest catch().

var Promise = require('promise');
var rp = require('request-promise');


function login(user, pass) {
    var options = {
        url: 'https://url.com/this',
        method: 'GET',
        headers: {
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
            'Referer': 'https://google.com/page'
        }
    };

    return rp(options)
        .then(function(html){
            var parsed = parseForm(html);
            parsed['post_user_name'] = user;
            parsed['post_user_pass'] = pass;
            return parsed;
        }).then(function(cred){
            var query   = querystring.stringify(cred);
            var opts = {
                url: 'https://url.com/next',
                method: 'POST',
                form: query,
                headers: {
                    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
                    'Referer': 'https://google.com/this'
                }
            };
            return rp(opts);
        }).then(function(success){
            var query   = querystring.stringify(cred);
            var opts = {
                url: 'https://url.com/loggedin',
                method: 'GET',
                headers: {
                    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
                    'Referer': 'https://google.com/loggedin'
                }
            };
            return rp(opts);
        }).then(function(success){
            console.log('here');
            var query = querystring.stringify(cred);
            var opts = {
                url: 'https://url.com/myaccount',
                method: 'GET',
                headers: {
                    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0',
                    'Referer': 'https://google.com/account'
                }
            };

            return rp(opts);
        })
        .then(function(body) {
            fs.writeFile('text.txt', body);
            return parseAuths(body);
        })
        .catch(function(err) {
            // Handle any errors;
            console.log(err);
        });           
    }
Comments