在async.forEach中未正确评估请求

时间:2017-04-01 10:51:32

标签: node.js request async.js

我想在request中请求async.forEach的多个网页。我有几个links,我想转到每个链接并从中获取sublinks。然后我想评估每个子链接并将结果推送到dataarray。但是对于每个sublink数组元素,只对第一个links进行了多次求值。但我想评估每个sublink。这是我的代码:

var dataarray = [];
var links = ["www.mysite.com/1","www.mysite.com/2","www.mysite.com/3"];

async.whilst(
    function() { console.log("Number of links left: " + links.length); return links.length > 0; }, 
    function(innerCallback){
        var i1 = getRandomInt(1, links.length); //getRandomInt is a function that gives a random integer in array.
        var Theurl = links[i1-1];
        request({
            url: Theurl,
            //More paramaters to request here...
        }, function(error, response, body) {        
            if (response) {
                //Evaluate some jquery to get a variable allsublinks used later on.
                //So I get several sub-links in each element in links array.
                var allsublinks =  stuff;

                //Now I define a function to be used in async.forEach. I basically need to
                //loop over allsublinks array that contains different url links.
                function getInfo(name, thiscallback) {
                    console.log("Online");
                    console.log("Current url: "+name);          
                    request({
                    url: name,
                    //Other parameters to request.
                    }, function(error, response, body) {
                        if (response) { 
                        //Here I evaluate some jquery again to get the datafromthislink variable
                        dataarray.push(datafromthislink); 
                        setTimeout(function() { thiscallback(); });                                                         
                    }
                    })
                }

                //Now I use async.forEach to loop over allstorelinks.
                async.forEach(allsublinks, getInfo, function(err, results) {
                    console.log("dataarray: ",dataarray);
                    setTimeout(function() { links.splice(i1-1, 1); innerCallback(); });
                });
            }
        })
    },
    function(err){
        console.log("All Done");
}) 

我做错了什么?

最好的问候

1 个答案:

答案 0 :(得分:0)

这可能是您想要实现的更清晰的版本,它清除了您正确使用回调的概念。

var dataarray = [];
var links = ["www.mysite.com/1", "www.mysite.com/2", "www.mysite.com/3"];

//Now I define a function to be used in async.forEach. I basically need to
//loop over allsublinks array that contains different url links.
function getInfo(link, infoCallback) {
    console.log("Online");
    console.log("Current url: " + link);
    request({
        url: link,
        //Other parameters to request.
    }, function (error, response, body) {
        if (error) console.log(error);
        if (response) {
            //Here I evaluate some jquery again to get the datafromthislink variable
            dataarray.push(body.datafromthislink);
        }
        infoCallback();
    })
}

function getLink(link, linkCallback) {
    console.log('checking link: ' + link);
    request({
        url: link,
        //More paramaters to request here...
    }, function (error, response, body) {
        if (error) console.log(error);
        if (response) {
            //Evaluate some jquery to get a variable allsublinks used later on.
            //So I get several sub-links in each element in links array.
            var allsublinks = body.stuff;
            //Now I use async.forEach to loop over allstorelinks.
            async.forEach(allsublinks, getInfo, linkCallback);
        } else {
            linkCallback();
        }
    })
}

async.each(links, getLink, function (err) {
    if (error) console.log(error);
    console.log("All Done");
    console.log(dataarray);
});