NodeJS Promise永远不会达到第三个'然后()'呼叫

时间:2016-05-12 18:08:43

标签: javascript node.js promise

我承诺我会处理登录我的某个网站,然后它应该收集一些信息。

我的代码:

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);
        });
    });
}

我的问题:承诺永远不会到达第三个then()函数。

同样,如果我在一个then()中编写包含所有请求调用的代码,然后尝试将我需要的数据传递给下一个,那么它也不会到达那里。

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
    });
}

我的问题:我做了什么明显的错误?有什么我想念的吗?

2 个答案:

答案 0 :(得分:1)

此方法使用request-promise。您可以将所有请求作为承诺,并使链保持原样。如果您遇到错误,它会正确传播到最近的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);
        });           
    }

答案 1 :(得分:-1)

您需要从then函数返回一个promise,并且还应该捕获在请求回调中返回的任何错误。试试这个:

}).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'
        }
    };
    // Need to return a promise
    return new Promise(function(resolve,reject) {
        request(options, function(err,httpResponse,body){
            if (err) { return reject(err); }
            return resolve('go again');
        });
    });