alsotang / node-lessons

:closed_book:《Node.js 包教不包会》 by alsotang
16.54k stars 4.7k forks source link

已解决‘部分’问题——没有块作用域的编程,不直接引用循环变量 : 原问题: 把.forEach() 换成 for() 循环为什么就不行了呢? 希望您有时间指点一下其中的错误 #108

Closed xuelingzhizun closed 8 years ago

xuelingzhizun commented 8 years ago
var eventproxy = require('eventproxy');
var superagent = require('superagent');
var cheerio = require('cheerio');
var url = require('url');
var cnodeurl = 'https://cnodejs.org/';
var ep=new eventproxy();
  superagent.get(cnodeurl)
    .end(function(err,sres){
       if(err){
          return console.error(err);
       }
       var topicUrls = [];
       var $ = cheerio.load(sres.text);
       $('#topic_list .topic_title').each(function(idx,element){
          var $element=$(element);
      var href = url.resolve(cnodeurl,$element.attr('href'));
      topicUrls.push(href);
       });
       ep.after('topic_html',topicUrls.length,function(content){
          content=content.map(function(topicPair){ 
             var topicUrl=topicPair[0];
         var topicHtml =topicPair[1];
         var $ = cheerio.load(topicHtml);
         return ({
            title: $('.topic_full_title').text().trim(),
            href: topicUrl,
            comment: $('.reply_content').eq(0).text().trim(),
              });
          });
        console.log('final');
        console.log(content);
       });

       /*  //你的代码 
       topicUrls.forEach(function(topicUrl){
       superagent.get(topicUrl)
         .end(function(err,res){
            console.log('fetch'+topicUrl+'successful');
        ep.emit('topic_html',[topicUrl,res.text]);
         });
    });
       */

     //我按照自己的理解写的
       for(var i=0;i<topicUrls.length;i++){
      console.log(i);   //Debug log
          superagent.get(topicUrls[i])
         .end(function(err,res){      
             console.log('fetch'+ topicUrls[i] + 'successful');
         ep.emit('topic_html',[topicUrls[i],res.text]);
         });
       }
}); 

运行结果是:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
fetchundefinedsuccessful
final
[ { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '', href: undefined, comment: '' },
  { title: '在用libuv 在读取文件内容时采用 uv_fs_read , 但一直不明白怎么用。看代码。',
    href: undefined,
    comment: '读文件内容的回调会被调用多次直到读取完毕' },
  { title: '精华\n\n\n\n        Electron简介',
    href: undefined,
    comment: '之前用 Electron 制作的一个分享文件的应用:\nhttps://github.com/psychokinesis-dev/free-share\n很简单,代码量只有几百行' },
  { title: 'Linux下gulp启动这个错误,谁遇到过。。。',
    href: undefined,
    comment: '我这今天也遇到这个问题了,现在正在排查,基本上能确定是某个版本的node module导致的。' },
  { title: 'node.js如何生成excel大文件呢?',
    href: undefined,
    comment: '用我的ejsExcel   模板引擎,试试,\n要文件实在太大,应该用流来生成CSV文件,这样,就不会有大小的限制了,\n比如,数据获得一条数据,就暂停,先写入CSV文件,然后再读取下一条数据,如此反复, 就不会有内存限制' },
  { title: '置顶\n\n\n\n        [ 北京]8月21日 NodeParty@科技寺,报名从速 !',
    href: undefined,
    comment: 'Threejs目前应用还是相当多的,大家有兴趣听听么?' },
  { title: 'request请求后输出的结果中id都为最后一个id',
    href: undefined,
    comment: '异步的问题,循环早完了,当回调执行时候,i就是2了' },
  { title: '通过github page上传demo后地址出现错误',
    href: undefined,
    comment: '' },
  { title: 'nodejs有解析Word(.doc)的包吗?',
    href: undefined,
    comment: '' },
  { title: '置顶\n\n\n\n        中国最大的JavaScript开发者大会JSConf China 2016即宁JS 售票啦!',
    href: undefined,
    comment: '具体演讲的安排能透露吗?' },
  { title: '虽然Vue2.0快来了,但我也利用1.0版本搭个CNode的Web App学习一下',
    href: undefined,
    comment: '页面返回后,就跑到顶部去了' },
  { title: '程序猿该如何学习英语?',
    href: undefined,
    comment: '同求。。。不知道外面的培训雅思是否靠谱,主要还是习惯的问题吧,码农的时间业余时间有限哎。。。。' },
  { title: 'SassDoc 详细介绍与最佳实践', href: undefined, comment: '' },
  { title: '关于mongoose认证踩的坑【url参数无法识别】',
    href: undefined,
    comment: '' } ]

还有为什么总是先打印 0 1 23 .. .... ..39 ,而不是 按照i=0时完全执行一次,在再i=1时在执行一次,直到执行到第39次?

谢谢的指教


\ 追加 ** 我好像找到了部分问题的原因-——————闭包,把

for(var i=0;i<topicUrls.length;i++){
35   
-     console.log(i);   //Debug log
36   
-          superagent.get(topicUrls[i])
37   
-        .end(function(err,res){      
38   
-            console.log('fetch'+ topicUrls[i] + 'successful');
39   
-        ep.emit('topic_html',[topicUrls[i],res.text]);
40   
-        });

        }

改成

for(var i=0;i<topicUrls.length;i++){
35   
-     console.log(i);   //Debug log
36   
    35
+         (function(n){
    36
+       console.log(n);   //Debug log
    37
+            superagent.get(topicUrls[n])
    38
+          .end(function(err,res){      
    39
+              console.log('fetch'+ topicUrls[n] + 'successful');
    40
+              ep.emit('topic_html',[topicUrls[n],res.text]);
    41
+          });
    42
+    })(i);
41  43
        }

\ 虽然结果没有 undefined 出现了,但是爬到的结果和源代码爬到的并不一致,而且我还是没理解为什么总是先打印 0 1 2 3 4 5 6...........39 **

几乎同一时刻不同源码爬到的数据

# 使用forEach爬到的
fetchhttps://cnodejs.org/topic/57bad9edb32043f532d40e1bsuccessful
fetchhttps://cnodejs.org/topic/57bad1276b04813133ba9083successful
fetchhttps://cnodejs.org/topic/57b57e9ae8db280a7c865175successful
fetchhttps://cnodejs.org/topic/57b7e25d012c43b704220afdsuccessful
fetchhttps://cnodejs.org/topic/57bac99ddcaeb5d932db224bsuccessful
fetchhttps://cnodejs.org/topic/57bacd1edcaeb5d932db2252successful
fetchhttps://cnodejs.org/topic/57b96a71b32043f532d40db7successful
fetchhttps://cnodejs.org/topic/57bac51c6b04813133ba9075successful
fetchhttps://cnodejs.org/topic/57ba968e6b04813133ba905dsuccessful
fetchhttps://cnodejs.org/topic/57ab1ec5ba44d08f4c1ce01dsuccessful
fetchhttps://cnodejs.org/topic/57bababf6b04813133ba906fsuccessful
fetchhttps://cnodejs.org/topic/57baac836b04813133ba9065successful
fetchhttps://cnodejs.org/topic/57baba451d27b9113348a49fsuccessful
fetchhttps://cnodejs.org/topic/57af4b84476898b472247ee9successful
fetchhttps://cnodejs.org/topic/57b0316dc4e3faf275fcb51csuccessful
fetchhttps://cnodejs.org/topic/57baad4f1d27b9113348a495successful
fetchhttps://cnodejs.org/topic/57bb0650dcaeb5d932db2267successful
fetchhttps://cnodejs.org/topic/57baad4d1d27b9113348a494successful
fetchhttps://cnodejs.org/topic/57baa91ddcaeb5d932db223bsuccessful
fetchhttps://cnodejs.org/topic/57b062ed144011da12ff4183successful
fetchhttps://cnodejs.org/topic/57b6850923d3109b04330491successful
fetchhttps://cnodejs.org/topic/57ba9cb6b32043f532d40df1successful
fetchhttps://cnodejs.org/topic/57a6a753ee053f647097252asuccessful
fetchhttps://cnodejs.org/topic/54afa85cce87bace2444ce44successful
fetchhttps://cnodejs.org/topic/57b9833fb32043f532d40dbasuccessful
fetchhttps://cnodejs.org/topic/57ba97196b04813133ba905esuccessful
fetchhttps://cnodejs.org/topic/57b3276fe8db280a7c8650ecsuccessful
fetchhttps://cnodejs.org/topic/57ba956a6b04813133ba905asuccessful
fetchhttps://cnodejs.org/topic/57ba8d3bdcaeb5d932db222bsuccessful
fetchhttps://cnodejs.org/topic/57bb053cdcaeb5d932db2265successful
fetchhttps://cnodejs.org/topic/575e70ef4a43c1cb159ffe25successful
fetchhttps://cnodejs.org/topic/57aac797ba44d08f4c1cdff3successful
fetchhttps://cnodejs.org/topic/57ba8fe1dcaeb5d932db222esuccessful
fetchhttps://cnodejs.org/topic/57bab091dcaeb5d932db223fsuccessful
fetchhttps://cnodejs.org/topic/57ba6a7a6b04813133ba903asuccessful
fetchhttps://cnodejs.org/topic/57812e64ee5f048d54f90800successful
fetchhttps://cnodejs.org/topic/57bade02b32043f532d40e1csuccessful
fetchhttps://cnodejs.org/topic/57bac3fbdcaeb5d932db2244successful
fetchhttps://cnodejs.org/topic/57b6b87d96d868d304386f69successful
fetchhttps://cnodejs.org/topic/57aab2fa476898b472247d99successful
final
[ { title: '',
    href: 'https://cnodejs.org/topic/57bad9edb32043f532d40e1b',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bad1276b04813133ba9083',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b57e9ae8db280a7c865175',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b7e25d012c43b704220afd',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bac99ddcaeb5d932db224b',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bacd1edcaeb5d932db2252',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b96a71b32043f532d40db7',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bac51c6b04813133ba9075',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba968e6b04813133ba905d',
    comment: '' },
  { title: '置顶\n\n\n\n        [ 北京]8月21日 NodeParty@科技寺,报名从速 !',
    href: 'https://cnodejs.org/topic/57ab1ec5ba44d08f4c1ce01d',
    comment: 'Threejs目前应用还是相当多的,大家有兴趣听听么?' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bababf6b04813133ba906f',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baac836b04813133ba9065',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baba451d27b9113348a49f',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57af4b84476898b472247ee9',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b0316dc4e3faf275fcb51c',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baad4f1d27b9113348a495',
    comment: '' },
  { title: 'fuck word inspect Model',
    href: 'https://cnodejs.org/topic/57bb0650dcaeb5d932db2267',
    comment: '赞!' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baad4d1d27b9113348a494',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baa91ddcaeb5d932db223b',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b062ed144011da12ff4183',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b6850923d3109b04330491',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba9cb6b32043f532d40df1',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57a6a753ee053f647097252a',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/54afa85cce87bace2444ce44',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b9833fb32043f532d40dba',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba97196b04813133ba905e',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b3276fe8db280a7c8650ec',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba956a6b04813133ba905a',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba8d3bdcaeb5d932db222b',
    comment: '' },
  { title: '成都求职,一年+经验',
    href: 'https://cnodejs.org/topic/57bb053cdcaeb5d932db2265',
    comment: '简历这儿——\n来自酷炫的 CNodeMD' },
  { title: '置顶\n\n\n\n        中国最大的JavaScript开发者大会JSConf China 2016即宁JS 售票啦!',
    href: 'https://cnodejs.org/topic/575e70ef4a43c1cb159ffe25',
    comment: '具体演讲的安排能透露吗?' },
  { title: '虽然Vue2.0快来了,但我也利用1.0版本搭个CNode的Web App学习一下',
    href: 'https://cnodejs.org/topic/57aac797ba44d08f4c1cdff3',
    comment: '页面返回后,就跑到顶部去了' },
  { title: '程序猿该如何学习英语?',
    href: 'https://cnodejs.org/topic/57ba8fe1dcaeb5d932db222e',
    comment: '同求。。。不知道外面的培训雅思是否靠谱,主要还是习惯的问题吧,码农的时间业余时间有限哎。。。。' },
  { title: 'Linux下gulp启动这个错误,谁遇到过。。。',
    href: 'https://cnodejs.org/topic/57bab091dcaeb5d932db223f',
    comment: '我这今天也遇到这个问题了,现在正在排查,基本上能确定是某个版本的node module导致的。' },
  { title: '在用libuv 在读取文件内容时采用 uv_fs_read , 但一直不明白怎么用。看代码。',
    href: 'https://cnodejs.org/topic/57ba6a7a6b04813133ba903a',
    comment: '读文件内容的回调会被调用多次直到读取完毕' },
  { title: '精华\n\n\n\n        Electron简介',
    href: 'https://cnodejs.org/topic/57812e64ee5f048d54f90800',
    comment: '之前用 Electron 制作的一个分享文件的应用:\nhttps://github.com/psychokinesis-dev/free-share\n很简单,代码量只有几百行' },
  { title: '通过github page上传demo后地址出现错误',
    href: 'https://cnodejs.org/topic/57bade02b32043f532d40e1c',
    comment: '' },
  { title: 'node.js如何生成excel大文件呢?',
    href: 'https://cnodejs.org/topic/57bac3fbdcaeb5d932db2244',
    comment: '用我的ejsExcel   模板引擎,试试,\n要文件实在太大,应该用流来生成CSV文件,这样,就不会有大小的限制了,\n比如,数据获得一条数据,就暂停,先写入CSV文件,然后再读取下一条数据,如此反复, 就不会有内存限制' },
  { title: '分享一个精简版react mobile ui库,希望有共同爱好的人一起来维护,包含且不限于react',
    href: 'https://cnodejs.org/topic/57b6b87d96d868d304386f69',
    comment: '打开控制台报这个错\nvendor.bundle.js:1 Warning: It looks like you\'re using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See https://fb.me/react-minification for more details.\n来自react-cnode手机版' },
  { title: 'Node-Orm2框架,高并发状态下导致内存暴涨的问题[已找到原因]',
    href: 'https://cnodejs.org/topic/57aab2fa476898b472247d99',
    comment: '什么原因呢' } ]

# 使用 for()循环的源码爬到的
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fetchhttps://cnodejs.org/topic/57ba6a7a6b04813133ba903asuccessful
fetchhttps://cnodejs.org/topic/57bac99ddcaeb5d932db224bsuccessful
fetchhttps://cnodejs.org/topic/57bb053cdcaeb5d932db2265successful
fetchhttps://cnodejs.org/topic/57baad4f1d27b9113348a495successful
fetchhttps://cnodejs.org/topic/57ba968e6b04813133ba905dsuccessful
fetchhttps://cnodejs.org/topic/57bad9edb32043f532d40e1bsuccessful
fetchhttps://cnodejs.org/topic/57b57e9ae8db280a7c865175successful
fetchhttps://cnodejs.org/topic/57b96a71b32043f532d40db7successful
fetchhttps://cnodejs.org/topic/57bacd1edcaeb5d932db2252successful
fetchhttps://cnodejs.org/topic/57baba451d27b9113348a49fsuccessful
fetchhttps://cnodejs.org/topic/57bad1276b04813133ba9083successful
fetchhttps://cnodejs.org/topic/57af4b84476898b472247ee9successful
fetchhttps://cnodejs.org/topic/57b6b87d96d868d304386f69successful
fetchhttps://cnodejs.org/topic/57b0316dc4e3faf275fcb51csuccessful
fetchhttps://cnodejs.org/topic/57bababf6b04813133ba906fsuccessful
fetchhttps://cnodejs.org/topic/57baac836b04813133ba9065successful
fetchhttps://cnodejs.org/topic/57baa91ddcaeb5d932db223bsuccessful
fetchhttps://cnodejs.org/topic/57baad4d1d27b9113348a494successful
fetchhttps://cnodejs.org/topic/57b6850923d3109b04330491successful
fetchhttps://cnodejs.org/topic/57ba9cb6b32043f532d40df1successful
fetchhttps://cnodejs.org/topic/57b062ed144011da12ff4183successful
fetchhttps://cnodejs.org/topic/57a6a753ee053f647097252asuccessful
fetchhttps://cnodejs.org/topic/57b9833fb32043f532d40dbasuccessful
fetchhttps://cnodejs.org/topic/54afa85cce87bace2444ce44successful
fetchhttps://cnodejs.org/topic/57ba8d3bdcaeb5d932db222bsuccessful
fetchhttps://cnodejs.org/topic/57b3276fe8db280a7c8650ecsuccessful
fetchhttps://cnodejs.org/topic/57ba956a6b04813133ba905asuccessful
fetchhttps://cnodejs.org/topic/57ba97196b04813133ba905esuccessful
fetchhttps://cnodejs.org/topic/575e70ef4a43c1cb159ffe25successful
fetchhttps://cnodejs.org/topic/57ab1ec5ba44d08f4c1ce01dsuccessful
fetchhttps://cnodejs.org/topic/57bab091dcaeb5d932db223fsuccessful
fetchhttps://cnodejs.org/topic/57bb0650dcaeb5d932db2267successful
fetchhttps://cnodejs.org/topic/57ba8fe1dcaeb5d932db222esuccessful
fetchhttps://cnodejs.org/topic/57bade02b32043f532d40e1csuccessful
fetchhttps://cnodejs.org/topic/57812e64ee5f048d54f90800successful
fetchhttps://cnodejs.org/topic/57bac51c6b04813133ba9075successful
fetchhttps://cnodejs.org/topic/57aac797ba44d08f4c1cdff3successful
fetchhttps://cnodejs.org/topic/57bac3fbdcaeb5d932db2244successful
fetchhttps://cnodejs.org/topic/57b7e25d012c43b704220afdsuccessful
fetchhttps://cnodejs.org/topic/57aab2fa476898b472247d99successful
final
[ { title: '',
    href: 'https://cnodejs.org/topic/57ba6a7a6b04813133ba903a',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bac99ddcaeb5d932db224b',
    comment: '' },
  { title: '成都求职,一年+经验',
    href: 'https://cnodejs.org/topic/57bb053cdcaeb5d932db2265',
    comment: '简历这儿——\n来自酷炫的 CNodeMD' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baad4f1d27b9113348a495',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba968e6b04813133ba905d',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bad9edb32043f532d40e1b',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b57e9ae8db280a7c865175',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b96a71b32043f532d40db7',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bacd1edcaeb5d932db2252',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baba451d27b9113348a49f',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bad1276b04813133ba9083',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57af4b84476898b472247ee9',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b6b87d96d868d304386f69',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b0316dc4e3faf275fcb51c',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57bababf6b04813133ba906f',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baac836b04813133ba9065',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baa91ddcaeb5d932db223b',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57baad4d1d27b9113348a494',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b6850923d3109b04330491',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba9cb6b32043f532d40df1',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b062ed144011da12ff4183',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57a6a753ee053f647097252a',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b9833fb32043f532d40dba',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/54afa85cce87bace2444ce44',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba8d3bdcaeb5d932db222b',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57b3276fe8db280a7c8650ec',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba956a6b04813133ba905a',
    comment: '' },
  { title: '',
    href: 'https://cnodejs.org/topic/57ba97196b04813133ba905e',
    comment: '' },
  { title: '置顶\n\n\n\n        中国最大的JavaScript开发者大会JSConf China 2016即宁JS 售票啦!',
    href: 'https://cnodejs.org/topic/575e70ef4a43c1cb159ffe25',
    comment: '具体演讲的安排能透露吗?' },
  { title: '置顶\n\n\n\n        [ 北京]8月21日 NodeParty@科技寺,报名从速 !',
    href: 'https://cnodejs.org/topic/57ab1ec5ba44d08f4c1ce01d',
    comment: 'Threejs目前应用还是相当多的,大家有兴趣听听么?' },
  { title: 'Linux下gulp启动这个错误,谁遇到过。。。',
    href: 'https://cnodejs.org/topic/57bab091dcaeb5d932db223f',
    comment: '我这今天也遇到这个问题了,现在正在排查,基本上能确定是某个版本的node module导致的。' },
  { title: 'fuck word inspect Model',
    href: 'https://cnodejs.org/topic/57bb0650dcaeb5d932db2267',
    comment: '赞!' },
  { title: '程序猿该如何学习英语?',
    href: 'https://cnodejs.org/topic/57ba8fe1dcaeb5d932db222e',
    comment: '同求。。。不知道外面的培训雅思是否靠谱,主要还是习惯的问题吧,码农的时间业余时间有限哎。。。。' },
  { title: '通过github page上传demo后地址出现错误',
    href: 'https://cnodejs.org/topic/57bade02b32043f532d40e1c',
    comment: '' },
  { title: '精华\n\n\n\n        Electron简介',
    href: 'https://cnodejs.org/topic/57812e64ee5f048d54f90800',
    comment: '之前用 Electron 制作的一个分享文件的应用:\nhttps://github.com/psychokinesis-dev/free-share\n很简单,代码量只有几百行' },
  { title: '关于ejs模板,forEach中怎么嵌套if语句',
    href: 'https://cnodejs.org/topic/57bac51c6b04813133ba9075',
    comment: '这个两行合成一行' },
  { title: '虽然Vue2.0快来了,但我也利用1.0版本搭个CNode的Web App学习一下',
    href: 'https://cnodejs.org/topic/57aac797ba44d08f4c1cdff3',
    comment: '页面返回后,就跑到顶部去了' },
  { title: 'node.js如何生成excel大文件呢?',
    href: 'https://cnodejs.org/topic/57bac3fbdcaeb5d932db2244',
    comment: '用我的ejsExcel   模板引擎,试试,\n要文件实在太大,应该用流来生成CSV文件,这样,就不会有大小的限制了,\n比如,数据获得一条数据,就暂停,先写入CSV文件,然后再读取下一条数据,如此反复, 就不会有内存限制' },
  { title: 'request请求后输出的结果中id都为最后一个id',
    href: 'https://cnodejs.org/topic/57b7e25d012c43b704220afd',
    comment: '异步的问题,循环早完了,当回调执行时候,i就是2了' },
  { title: 'Node-Orm2框架,高并发状态下导致内存暴涨的问题[已找到原因]',
    href: 'https://cnodejs.org/topic/57aab2fa476898b472247d99',
    comment: '什么原因呢' } ]
alsotang commented 8 years ago

搜【js for 闭包 引用】

2016-08-22 20:52 GMT+08:00 xuefeng notifications@github.com:

var eventproxy = require('eventproxy'); var superagent = require('superagent'); var cheerio = require('cheerio'); var url = require('url'); var cnodeurl = 'https://cnodejs.org/'; var ep=new eventproxy(); superagent.get(cnodeurl) .end(function(err,sres){ if(err){ return console.error(err); } var topicUrls = []; var $ = cheerio.load(sres.text); $('#topic_list .topic_title').each(function(idx,element){ var $element=$(element); var href = url.resolve(cnodeurl,$element.attr('href')); topicUrls.push(href); }); ep.after('topic_html',topicUrls.length,function(content){ content=content.map(function(topicPair){ var topicUrl=topicPair[0]; var topicHtml =topicPair[1]; var $ = cheerio.load(topicHtml); return ({ title: $('.topic_full_title').text().trim(), href: topicUrl, comment: $('.reply_content').eq(0).text().trim(), }); }); console.log('final'); console.log(content); });

   /*  //你的代码
   topicUrls.forEach(function(topicUrl){
   superagent.get(topicUrl)
     .end(function(err,res){
        console.log('fetch'+topicUrl+'successful');
    ep.emit('topic_html',[topicUrl,res.text]);
     });
});
   */

 //我按照自己的理解写的
   for(var i=0;i<topicUrls.length;i++){
  console.log(i);   //Debug log
      superagent.get(topicUrls[i])
     .end(function(err,res){
         console.log('fetch'+ topicUrls[i] + 'successful');
     ep.emit('topic_html',[topicUrls[i],res.text]);
     });
   }

});

运行结果是:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful fetchundefinedsuccessful final [ { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '', href: undefined, comment: '' }, { title: '在用libuv 在读取文件内容时采用 uv_fs_read , 但一直不明白怎么用。看代码。', href: undefined, comment: '读文件内容的回调会被调用多次直到读取完毕' }, { title: '精华\n\n\n\n Electron简介', href: undefined, comment: '之前用 Electron 制作的一个分享文件的应用:\nhttps://github.com/psychokinesis-dev/free-share\n http://github.com/psychokinesis-dev/free-share%5Cn很简单,代码量只有几百行' }, { title: 'Linux下gulp启动这个错误,谁遇到过。。。', href: undefined, comment: '我这今天也遇到这个问题了,现在正在排查,基本上能确定是某个版本的node module导致的。' }, { title: 'node.js如何生成excel大文件呢?', href: undefined, comment: '用我的ejsExcel 模板引擎,试试,\n要文件实在太大,应该用流来生成CSV文件,这样,就不会有大小的限制了,\n比如,数据获得一条数据,就暂停,先写入CSV文件,然后再读取下一条数据,如此反复, 就不会有内存限制' }, { title: '置顶\n\n\n\n [ 北京]8月21日 NodeParty@科技寺,报名从速 !', href: undefined, comment: 'Threejs目前应用还是相当多的,大家有兴趣听听么?' }, { title: 'request请求后输出的结果中id都为最后一个id', href: undefined, comment: '异步的问题,循环早完了,当回调执行时候,i就是2了' }, { title: '通过github page上传demo后地址出现错误', href: undefined, comment: '' }, { title: 'nodejs有解析Word(.doc)的包吗?', href: undefined, comment: '' }, { title: '置顶\n\n\n\n 中国最大的JavaScript开发者大会JSConf China 2016即宁JS 售票啦!', href: undefined, comment: '具体演讲的安排能透露吗?' }, { title: '虽然Vue2.0快来了,但我也利用1.0版本搭个CNode的Web App学习一下', href: undefined, comment: '页面返回后,就跑到顶部去了' }, { title: '程序猿该如何学习英语?', href: undefined, comment: '同求。。。不知道外面的培训雅思是否靠谱,主要还是习惯的问题吧,码农的时间业余时间有限哎。。。。' }, { title: 'SassDoc 详细介绍与最佳实践', href: undefined, comment: '' }, { title: '关于mongoose认证踩的坑【url参数无法识别】', href: undefined, comment: '' } ]

还有为什么总是先打印 0 1 23 .. .... ..39 ,而不是 按照i=0时完全执行一次,在再i=1时在执行一次,直到执行到第39次?

谢谢的指教

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/alsotang/node-lessons/issues/108, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGB7_Pa5_5iNRBh-XNG_cEQVPtjyOxjks5qiZuWgaJpZM4Jp1Ul .