【问题标题】:NodeJS. Async. Parallel. Same functions节点JS。异步。平行。相同的功能
【发布时间】:2015-12-22 11:03:00
【问题描述】:

我需要解析 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);
}

【问题讨论】:

    标签: javascript node.js asynchronous


    【解决方案1】:

    您可以使用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());
        });
    
    };
    

    最好的问候, 叶戈尔

    【讨论】:

      【解决方案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);
      });
      

      【讨论】:

      • 你在这里使用co。单独的生成器函数没有任何帮助。
      • @Bergi,我的措辞不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 2020-03-30
      • 2015-08-19
      相关资源
      最近更新 更多