Closed xuelingzhizun closed 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 .
运行结果是:
还有为什么总是先打印 0 1 23 .. .... ..39 ,而不是 按照i=0时完全执行一次,在再i=1时在执行一次,直到执行到第39次?
谢谢的指教
\ 追加 ** 我好像找到了部分问题的原因-——————闭包,把
改成
\ 虽然结果没有 undefined 出现了,但是爬到的结果和源代码爬到的并不一致,而且我还是没理解为什么总是先打印 0 1 2 3 4 5 6...........39 **
几乎同一时刻不同源码爬到的数据
和