首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何异步执行多个Nightmare函数

如何异步执行多个Nightmare函数
EN

Stack Overflow用户
提问于 2015-10-13 00:32:17
回答 1查看 1.1K关注 0票数 1

我正在用Nightmare抓取一个网页,想知道如何在输入数组上重用一个函数。

假设我有一个检索页面标题的方法

代码语言:javascript
复制
function* test(url,callback) {
    var size = { width: 1920, height: 1080, 'use-content-size': true, show: true }

    var nightmare = Nightmare(size)
    var title = yield nightmare
    .goto('http://cnn.com')
    .evaluate(function () {
        return document.title
    });
    console.log(title)
    yield nightmare.end()
    callback()
}

我想在urls数组上执行此方法。因此,我使用异步库来遍历整个数组,并对urls的urls数组执行test函数。

代码语言:javascript
复制
async.each(urls, test, function (err) {
    console.log('done!');
});

但是async.each不支持生成器函数,如何将测试函数更改为普通函数而不是生成器函数。

EN

回答 1

Stack Overflow用户

发布于 2016-01-11 03:06:12

我已经找到了一种方法-我需要使用promises库来执行多个函数,这是一个普通的函数。

代码语言:javascript
复制
function test(url){
    Promise.resolve(  // call for the promises library with the nightmare instance 
        nightmare
        .goto() //all the calls that you need
        .wait()
        .....    
        ).then(function (result) { //This function will be called when the block of commands is done , with the result.
              console.log("Done")
        }, function (err) { //Error handling 
              console.log(err);
    });
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33085985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档