的NodeJS。异步。平行。功能相同

时间:2015-12-22 11:03:00

标签: javascript node.js asynchronous

我需要解析10个网页,并抓住他们的主要内容。所以我使用节点可读性并且不想重写相同的功能(仅限网址更改)10次。最后,我要计算内容长度。我如何使用循环或任何其他想法来做到这一点? 现在看起来像:

for(var i=0; i<catchedUrl.length; i++){
    var data = {length: 0, count: 0};
    (function(i) {
        read(catchedUrl[i], function(err, article, meta){
            if(err) throw err;

            var content = article.content;
            content = content.split(' ');
            article.close();
            data.count += 1;
            data.length += length;
            // Send data to callback when functions done
        });
    })(i);
}

2 个答案:

答案 0 :(得分:3)

您可以使用async模块来简化循环。另请查看.bind()函数bind documentation

因此,对这种情况的代码示例可能看起来像那样

var async = require('async');

function step(number, callback) {
     [enter code here]
     callback();
}

module.exports = (job, done) => {
    var _pages = [URLS];
        async.eachSeries(_pages, (link, callback)=> {
            step(link, callback);
        }, ()=> done());
    });

};

祝你好运, 叶戈尔

答案 1 :(得分:2)

Egor的回答很有效。

你也可以利用co摆脱异步性:

$ npm i --save co thunkify

var co = require('co');
var read = require('node-readability');
var thunkify = require('thunkify');

var cachedUrls = [
    'http://stackoverflow.com/questions/34414539/elasticsearch-filtering-mulitple-documents-with-same-term',
    'http://stackoverflow.com/questions/34414537/selecting-multiple-values-with-multiple-where-clauses',
    'http://stackoverflow.com/questions/34414536/how-to-create-functional-test-directory-in-grails',
    'http://stackoverflow.com/questions/34414534/azure-active-directory-application-key-renewal',
    'http://stackoverflow.com/questions/34414532/store-facebook-credential-in-android-for-google-smart-lock-password',
    'http://stackoverflow.com/questions/34414531/ssis-read-flat-file-skip-first-row',
    'http://stackoverflow.com/questions/34414529/set-non-database-attribute-for-rails-model-without-attr-accessor',
    'http://stackoverflow.com/questions/34414525/excel-code-blocking-other-excel-sheets-to-open',
    'http://stackoverflow.com/questions/34414522/app-crash-when-network-connection-gone',
    'http://stackoverflow.com/questions/34414520/nest-input-inside-label-with-simple-form-and-rails-4'
];

co(function *() {

    var data = { 
        length: 0, 
        count: 0
    };

    for (var i = 0, n = cachedUrls.length; i < n; i++) {

        let response = yield thunkify(read)(cachedUrls[i]);

        data.length += response['0'].content.split(' ').length;
        data.count++;       
    }

    return data;

}).then(function(value) {
    console.log('final value:', value);
});