如何在node js中进行异步调用
How to make a synchronus calls in nodejs
我正在尝试将 excel 文件数据从文件解析为 mysql 数据库,但我会为许多文件执行此操作,但由于 nodejs 在完成任务之前的异步 属性第一个文件它会中断第二个文件,所以有什么方法可以使此功能同步。
我的nodejs代码:
while (typeof issue.fields.attachment[r] != "undefined") {
if (typeof issue.fields.attachment[r].content != "undefined") {
var url = issue.fields.attachment[r].content;
console.log('url :' + url);
request({
method: "GET",
"rejectUnauthorized": false,
"url": url,
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic" +
}
},
function(err, data, body) {
//console.log(data.body);
console.log('file downloading');
}).pipe(fs.createWriteStream('file.xlsx'));
console.log('file downloaded');
parseXlsx('file.xlsx', function(err, data) {});
}
}
您可以使用异步的 while 方法很容易地使 while 调用的内容连续执行。您将条件包装在匿名函数中并将其作为第一个参数传递,将 while 的内容包装在另一个匿名函数中并将其作为第二个参数传递。然后,当你每次完成 while 的工作时,你都会调用回调,这就是它知道循环的方式。
var async = require('async');
async.whilst(function(){return typeof issue.fields.attachment[r] !== "undefined"},function(callback){
if(typeof issue.fields.attachment[r].content != "undefined")
{
var url = issue.fields.attachment[r].content ;
console.log('url :'+url);
var wstream = fs.createWriteStream('file.xlsx');
request({
method: "GET",
"rejectUnauthorized": false,
"url": url,
"headers" :{
"Content-Type": "application/json",
"Authorization": "Basic"+
}
},function(err,data,body){
//console.log(data.body);
console.log('file downloading');
}).pipe(wstream);
wstream.on('finish',function(){
parseXlsx('file.xlsx', function(err, data){
return callback();
});
});
}
}
您需要进行一些错误处理。如果你调用 callback() 时出现错误,它将停止循环,所以如果你希望它在发生错误的情况下继续,你只需调用 callback();
注意:我还修复了您的一些 writestream 代码。
您可以为每个希望连续执行的任务创建 Promise。
然后它只是一个简单的串联执行任务的承诺。
我正在尝试将 excel 文件数据从文件解析为 mysql 数据库,但我会为许多文件执行此操作,但由于 nodejs 在完成任务之前的异步 属性第一个文件它会中断第二个文件,所以有什么方法可以使此功能同步。
我的nodejs代码:
while (typeof issue.fields.attachment[r] != "undefined") {
if (typeof issue.fields.attachment[r].content != "undefined") {
var url = issue.fields.attachment[r].content;
console.log('url :' + url);
request({
method: "GET",
"rejectUnauthorized": false,
"url": url,
"headers": {
"Content-Type": "application/json",
"Authorization": "Basic" +
}
},
function(err, data, body) {
//console.log(data.body);
console.log('file downloading');
}).pipe(fs.createWriteStream('file.xlsx'));
console.log('file downloaded');
parseXlsx('file.xlsx', function(err, data) {});
}
}
您可以使用异步的 while 方法很容易地使 while 调用的内容连续执行。您将条件包装在匿名函数中并将其作为第一个参数传递,将 while 的内容包装在另一个匿名函数中并将其作为第二个参数传递。然后,当你每次完成 while 的工作时,你都会调用回调,这就是它知道循环的方式。
var async = require('async');
async.whilst(function(){return typeof issue.fields.attachment[r] !== "undefined"},function(callback){
if(typeof issue.fields.attachment[r].content != "undefined")
{
var url = issue.fields.attachment[r].content ;
console.log('url :'+url);
var wstream = fs.createWriteStream('file.xlsx');
request({
method: "GET",
"rejectUnauthorized": false,
"url": url,
"headers" :{
"Content-Type": "application/json",
"Authorization": "Basic"+
}
},function(err,data,body){
//console.log(data.body);
console.log('file downloading');
}).pipe(wstream);
wstream.on('finish',function(){
parseXlsx('file.xlsx', function(err, data){
return callback();
});
});
}
}
您需要进行一些错误处理。如果你调用 callback() 时出现错误,它将停止循环,所以如果你希望它在发生错误的情况下继续,你只需调用 callback();
注意:我还修复了您的一些 writestream 代码。
您可以为每个希望连续执行的任务创建 Promise。 然后它只是一个简单的串联执行任务的承诺。