Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async流程控制 #12

Open
raclen opened this issue Sep 15, 2019 · 0 comments
Open

async流程控制 #12

raclen opened this issue Sep 15, 2019 · 0 comments
Labels
博客搬迁 以前博客的文章迁移过来

Comments

@raclen
Copy link
Owner

raclen commented Sep 15, 2019

写爬虫的时候会用上
代码都是异步执行,也是个麻烦事。
当for循环中执行函数,是异步的,就会出现问题,上次用了一个很蹩脚的办法,把循环改成定时器

$.each(function (i, item) {
    self.core(item, backfun); //异步函数
});

改成了这样

var count = 0;
var timeout1 = setInterval(function () {
    if (count == arr.length) {
        clearInterval(timeout1);
        return;
    }
    var index = count % arr.length;
    self.core(arr[index], backfun);
    count++;
}, 1000);

这样还是不好,下面用async来控制流程....
使用到了async.eachSeries,注意它的第二个函数,不光是用来处理错误的,也是回调的,有了它就可以连起来了

async.eachSeries(arr, function (item, callback) {
    console.log(item)
    self.core(item, function ($) {
        var $itemlist = $('.post-listing .item-list');
        var linkList = $itemlist.map(function (idx, element) {
            var $element = $(element).find('.post-title a');
            return $element.attr('href');
        }).get();
        console.log(linkList)
        async.eachSeries(linkList, function (item, callback2) {
            self.core(item, function ($) {
                var author = $('.post-inner .entry').find('p').eq(0).text();
                var content = $.html('.article_text');
                var text = $('.post-inner span[itemprop="name"]').text();
                console.log("text=" + text);
                if ( !! text) {
                    var thor = new articlema({
                        title: text,
                        author: author,
                        category: '002',
                        content: content
                    });
                    thor.save(function (err, thor) {
                        if (err) return console.log(err);
                        //console.log(thor);
                    });
 
                }
                sleep(500)
                callback2(null)
            });
        }, function (err) {
            console.log("content==============" + err);
            sleep(500)
            callback(null)
        });
    })
 
}, function (err) {
    console.log("item==============" + err);
})


image

#2016-02-04

@raclen raclen added the 博客搬迁 以前博客的文章迁移过来 label Sep 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
博客搬迁 以前博客的文章迁移过来
Projects
None yet
Development

No branches or pull requests

1 participant