简体   繁体   English

Node.js中使用异步模块的问题

[英]issue using async module in Node.js

Having an issue, appreciate any help. 遇到问题时,感谢您的帮助。

I'm trying to gather all my async functions together. 我正在尝试将所有异步功能收集在一起。 Tried async.parallel, async.each, gather-gm. 尝试了async.parallel,async.each,gather-gm。 Nothing makes the final callback work. 没有任何事情使最终的回调工作。 Here's the updated code(but yet not working properly): 这是更新的代码(但仍无法正常工作):

var calls = [];

async.each(parser.allHrefs,function (href,callback) {

    getHtml(href,function(err, add){
        console.log("Passing data: " + href);

        if(err){
            return callback(err);
        };

        if(add){
            calls.push(href);
        };

        return callback();
    });
}, function (err) {
    if(err){
        console.log('something went wrong');
    }else{
        console.log('finished');
    };
}); 

And the first function: 和第一个功能:

function getHtml(link, callback) {    
  httpreq.get(link, function(err, res) {

    if(err) {            
      return callback(err);      
    }

    if(res.statusCode >= 300) {      
      return callback(null, false);       
    } else {
      //cut parsing code here...
      return callback(null, true);          
    }
  });
}

ps:I've updated the code couple times. ps:我已经更新了几次代码。 In this example i've tried to use async.parallel. 在此示例中,我尝试使用async.parallel。 And the thing is when i even get no errors, i still dont get to the "getLocations" function. 事情是,当我什至没有出错时,我仍然没有到达“ getLocations”功能。 Code looks much more better than first versions, but still refuses to work correctly. 代码看起来比第一个版本要好得多,但是仍然无法正常工作。

There are several issues in your gethtml function you need to fix. 您的gethtml函数中有几个问题需要解决。

Make sure that everywhere you have now return, you call the callback, eg 确保现在返回的所有地方都调用回调,例如

return callback(err)  

when you want to communicate an error or 当您想传达错误或

return callback(null,  result) 

when you want to communicate success and return a result. 当您想传达成功并返回结果时。 Never return without calling the callback. 切勿在未调用回调的情况下返回。

Instead of deleting the parser.allHrefs while you go through it. 而不是在您通过它时删除parser.allHrefs You should refactor de getHtml just to know if you can add the link or not. 您应该重构de getHtml只是为了知道是否可以添加链接。 Like this: 像这样:

function getHtml (link, callback) {    
    httpreq.get(link, function(err, res) {
        //the request has already finished executing here
        if(err) {      
            return callback(err);      
        }

        if(res.statusCode >= 300) {
            return callback(null,false);
        } else {
            //cut parsing code here...
            return callback(null,true);
        }
    });
};

This way when you call the gatherSecondLevelData function you check if you add the link or not like this: 这样,当您调用gatherSecondLevelData函数时,将检查是否添加链接,如下所示:

function gatherSecondLevelData (err) {
    var calls = [];

    async.each(parser.allHrefs,function (href,callback) {

        getHtml(href,function(err, add){

            if(err){
                return callback(err);
            };

            if(add){
                calls.push(href);
            };

            return callback();
        });
    }, function (err) {
        if(err){
            console.log('something went wrong');
        }else{
            console.log('finished');
        };
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM