Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 点头。异步的。平行的相同功能_Javascript_Node.js_Asynchronous - Fatal编程技术网

Javascript 点头。异步的。平行的相同功能

Javascript 点头。异步的。平行的相同功能,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我需要解析10个网页,并捕捉它们的主要内容。所以我使用的是节点可读性,不想重写相同的函数(仅url更改)10次。最后,我必须计算内容长度。我如何使用循环或任何其他想法来实现它? 现在看来: for(var i=0; i<catchedUrl.length; i++){ var data = {length: 0, count: 0}; (function(i) { read(catchedUrl[i], function(err, article, meta

我需要解析10个网页,并捕捉它们的主要内容。所以我使用的是节点可读性,不想重写相同的函数(仅url更改)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);
}

for(var i=0;i您可以使用
async
模块简化循环。另外,请查看
.bind()
函数

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

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

};
致以最良好的祝愿,
伊戈尔的回答很有效

您还可以使用
co
来消除异步性:

$npm i--保存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);
});
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'
];
文书主任(职能*(){
变量数据={
长度:0,
计数:0
};
for(var i=0,n=cachedUrls.length;i
您在这里使用的是
co
。生成器函数本身没有任何帮助。@Bergi,我用了不同的措辞。