Node.js强制等待功能完成

汉娜·墨菲(Hannah Murphy)

我在使用Node.js运行的程序中有一个for循环。该函数是xray包中的x(),我正在使用它来从网页上抓取并接收数据,然后将该数据写入文件。该程序在用于刮擦〜100页时是成功的,但是我需要刮擦〜10000页。当我尝试刮取大量页面时,将创建文件,但它们不保存任何数据。我相信这个问题存在是因为for循环在进入下一个迭代之前没有等待x()返回数据。

有没有一种方法可以让节点在继续下一次迭代之前等待x()函数完成?

//takes in file of urls, 1 on each line, and splits them into an array. 
//Then scrapes webpages and writes content to a file named for the pmid number that represents the study
 
//split urls into arrays
var fs = require('fs');
var array = fs.readFileSync('Desktop/formatted_urls.txt').toString().split("\n");


var Xray = require('x-ray');
var x = new Xray();
 
for(i in array){
        //get unique number and url from the array to be put into the text file name
                number = array[i].substring(35);
                url = array[i];


        //use .write function of x from xray to write the info to a file
        x(url, 'css selectors').write('filepath' + number + '.txt');
                               
}

注意:我要抓取的某些页面没有返回任何值

大学教师

代码的问题是您没有等待将文件写入文件系统。与逐个下载文件相比,一种更好的方法是一次性完成文件,然后等待文件完成,而不是逐个处理文件,然后再进行下一个文件的下载。

推荐的用于处理Node.js中的Promise的库之一是bluebird。

http://bluebirdjs.com/docs/getting-started.html

在更新后的示例中(请参见下文),我们遍历所有URL并开始下载,并跟踪承诺,然后在写入文件后便解决了每个承诺。最后,我们只是等待使用Promise.all()解决所有承诺

这是更新的代码:

var promises = [];
var getDownloadPromise = function(url, number){
    return new Promise(function(resolve){
        x(url, 'css selectors').write('filepath' + number + '.txt').on('finish', function(){
            console.log('Completed ' + url);
            resolve();
        });
    });
};

for(i in array){
    number = array[i].substring(35);
    url = array[i];

    promises.push(getDownloadPromise(url, number));                               
}

Promise.all(promises).then(function(){
    console.log('All urls have been completed');
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Node.js中的功能编程-等待功能完成

强制Javascript / Node等待服务完成

如何强制后处理等待所有提取过程在node.js中完成?

等待功能完成后再执行下一步-Node JS

Node.js在执行功能之前需要等待循环完成

等待两个异步功能完成,然后在Node.js中继续

Node js:等待 100 个请求完成

等待操作在node.js中完成

等待请求完成Node.js

node.js等待文件操作完成

异步和等待功能Node.js

Node.js-Passport,完成不是功能

是什么导致node.js等待请求完成?

node.js继续执行而无需等待要求完成。

node.js:等待所有线程完成

Node.JS等待每个并行mysql查询完成

Node.js等待for循环完成以mysql查询

Node.JS:如何在继续之前等待进程完成?

node js async & await 函数 - 强制函数完成?

SyntaxError:意外的令牌功能-异步等待Node.js

等待所有不同的承诺完成Node.js(异步等待)

带有 node-cmd 的 Node JS:等待前一个 cmd 调用的执行完成

Node.JS 在启动其他命令之前等待 node-cmd run 命令完成

JS(node.js)-如果我进行异步调用但不等待其完成,是否可以保证完成执行?

等待文件完成写入磁盘的操作,然后再在Node.js中进行处理

如何在Node.js中等待所有异步任务完成?

Node.js:节点是否总是在退出之前等待setTimeout()完成?

Node.js,等待所有Redis查询完成,然后再继续执行

如何在node.js中等待带有异步请求的循环完成?