alsotang / node-lessons

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

lession4的例子 #112

Closed qinyou closed 7 years ago

qinyou commented 8 years ago

40个 url 异步请求(superagent.get()),然后 等 得到( 攒足)40个 document 后 再 顺序解析(array.map())? 这样 的 “异步并发” 有意义吗?

alsotang commented 8 years ago

呃。。。不懂怎么回答你这问题。

你的意思是,抓一个就马上解析一个这样比较快还是?

2016-08-30 22:02 GMT+08:00 Chuang notifications@github.com:

40个 url 异步请求(superagent.get()),然后 等 得到( 攒足)40个 document 后 再 顺序解析(array.map())? 这样 的 “异步并发” 有意义吗?

— 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/112, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGB7-Pu3S1aWtruCDbOgWB4zPQRoEt3ks5qlDgAgaJpZM4Jwj5x .

mengLLLL commented 7 years ago

同样的问题……这样怎么就算“异步”?怎么体现的? 40个url是一个一个顺序访问的,然后等所有的都执行完,再去解析数据,异步体现在哪里?求大神解答:)

fundon commented 7 years ago

urls 是个有序的数组,发起的请求也是有序,到返回的时候则无序。

mengLLLL commented 7 years ago

哦哦,就是返回的时候体现的异步是么,多谢:)

bigggge commented 7 years ago

@fundon 那么怎么在异步请求的情况下,发起的请求有序,到返回的时候数据顺序和请求顺序一样呢?

alsotang commented 7 years ago

eventproxy 的 group 方法可以做到

2016-10-09 10:19 GMT+08:00 bigggge notifications@github.com:

@fundon https://github.com/fundon 那么怎么在异步请求的情况下,发起的请求有序, 到返回的时候数据顺序和请求顺序一样呢?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alsotang/node-lessons/issues/112#issuecomment-252459950, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGB70nTLEN6dNprsk8Pu993RqkKYNxJks5qyE9FgaJpZM4Jwj5x .

bigggge commented 7 years ago

为什么这样使用group没有效果,没有执行console.log(topics);

var ep = new eventproxy();
        ep.after('topic_html', topicUrls.length, function (topics) {

            topics = topics.map(function (topicPair) {
                var topicUrl = topicPair[0];
                var topicHtml = topicPair[1];
                var $ = cheerio.load(topicHtml);
                return ({
                    title: $('.topic_full_title').text().trim(),
                    href: topicUrl,
                    comments1: $('.reply_content').eq(0).text().trim()
                });
            });
            console.log('final:');
            console.log(topics);
        });

        topicUrls.forEach(function (topicUrl) {
            superagent.get(topicUrl)
            // .end(function (err, res) {
            //     console.log('fetch ' + topicUrl + ' successful');
            //     eq.emit('topic_html', [topicUrl, res.text]);
            // });
                .end(ep.group('topic_html', function (data) {
                    return data;
                }));
        });
fundon commented 7 years ago

@bigggge 数组是有序结构,所以有序体现在,发起请求时最终收集到的结果;而无序则体现在接收结果时

fundon commented 7 years ago

举个简单例子:

要联络几个人,有个名单列表,一个个给他们发短信,这时就是有序的触发;但不知道他们什么时候回复,这时就会出现无序的状况了;如果有人回复,就在名单请打 ✅ ,那么等所有人都回复了,最终看到的就是一个都 ✅ 的有序名单列表了。

bigggge commented 7 years ago

@fundon 我的意思是代码的实现

alsotang commented 7 years ago

ep.fail 看看报错

2016-10-09 12:38 GMT+08:00 bigggge notifications@github.com:

@fundon https://github.com/fundon 我的意思是代码的实现

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alsotang/node-lessons/issues/112#issuecomment-252464289, or mute the thread https://github.com/notifications/unsubscribe-auth/ABGB74KwcpNtjJAx1MhXpc9fIIE-IvEfks5qyG-xgaJpZM4Jwj5x .