Nodejs发送瀑布请求
Nodejs send waterfall requests
我有带文件夹名称的动态数组,并使用 zipFolder 创建了一个 zip
由于库是异步的,我遇到了问题,即
zipFolder(source,dest,callback)
因为它是异步调用,所以需要时间,我想阻止下一个请求,除非第一个响应为真,
function (req, res)
var folder = ['N','PP','Y'];//dynamic just an example
for( var i = 0 ; i < folder.length ; i++) {
var source = path.join(basePath,'/download/'+folder[i]);
var dest = path.join(basePath,'/createZip/'+folder[i]+'.zip');
console.log('sending',folders[i])
//this is async
zipFolder(source,dest, function(err) {
if(err) {
console.log('oh no!', err);
} else {
console.log('EXCELLENT');//want to send the second,third,fourth if success
}
});
}
总结:
输出应该是这样的:
发送文件 1 if success/response 发送文件 2 if success/response
想要瀑布之类的东西等待回调响应发送第二个文件
另外我想要一个方法来知道完成回调所以我可以发回响应
尝试使用异步 waterfall
方法
https://caolan.github.io/async/docs.html#waterfall
Runs the tasks array of functions in series, each passing their
results to the next in the array. However, if any of the tasks pass an
error to their own callback, the next function is not executed, and
the main callback is immediately called with the error
async.waterfall([
function(callback) {
callback(null, 'one', 'two');
},
function(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
},
function(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
更新
在你的用例中 eachSeries
更适用。
var folder = ['N','PP','Y'];
async.eachSeries(folder, function(item,callback){
var source = path.join(basePath,'/download/'+item);
var dest = path.join(basePath,'/createZip/'+item+'.zip');
zipFolder(source,dest, function(err) {
if(err) {
console.log('oh no!', err);
callback();
} else {
console.log('EXCELLENT');//want to send the second,third,fourth if success
callback();
}
});
},function(){
console.log('all items uploaded');
}); .
你可以像这样使用每一个:
var each = require('async-each-series')
var folder = ['N','PP','Y'];
each(folder, function(items,callback){
//insert each items to db in async way
var source = path.join(basePath,'/download/'+items);
var dest = path.join(basePath,'/createZip/'+items+'.zip');
zipFolder(source,dest, function(err) {
if(err) {
console.log('oh no!', err);
} else {
console.log(items,'inserted')
callback()
}
});
},function(err){
if(err) console.log('error occured')
console.log('all items uploaded');
});
我有带文件夹名称的动态数组,并使用 zipFolder 创建了一个 zip 由于库是异步的,我遇到了问题,即
zipFolder(source,dest,callback)
因为它是异步调用,所以需要时间,我想阻止下一个请求,除非第一个响应为真,
function (req, res)
var folder = ['N','PP','Y'];//dynamic just an example
for( var i = 0 ; i < folder.length ; i++) {
var source = path.join(basePath,'/download/'+folder[i]);
var dest = path.join(basePath,'/createZip/'+folder[i]+'.zip');
console.log('sending',folders[i])
//this is async
zipFolder(source,dest, function(err) {
if(err) {
console.log('oh no!', err);
} else {
console.log('EXCELLENT');//want to send the second,third,fourth if success
}
});
}
总结: 输出应该是这样的:
发送文件 1 if success/response 发送文件 2 if success/response
想要瀑布之类的东西等待回调响应发送第二个文件
另外我想要一个方法来知道完成回调所以我可以发回响应
尝试使用异步 waterfall
方法
https://caolan.github.io/async/docs.html#waterfall
Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error
async.waterfall([
function(callback) {
callback(null, 'one', 'two');
},
function(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
},
function(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}
], function (err, result) {
// result now equals 'done'
});
更新
在你的用例中 eachSeries
更适用。
var folder = ['N','PP','Y'];
async.eachSeries(folder, function(item,callback){
var source = path.join(basePath,'/download/'+item);
var dest = path.join(basePath,'/createZip/'+item+'.zip');
zipFolder(source,dest, function(err) {
if(err) {
console.log('oh no!', err);
callback();
} else {
console.log('EXCELLENT');//want to send the second,third,fourth if success
callback();
}
});
},function(){
console.log('all items uploaded');
}); .
你可以像这样使用每一个:
var each = require('async-each-series')
var folder = ['N','PP','Y'];
each(folder, function(items,callback){
//insert each items to db in async way
var source = path.join(basePath,'/download/'+items);
var dest = path.join(basePath,'/createZip/'+items+'.zip');
zipFolder(source,dest, function(err) {
if(err) {
console.log('oh no!', err);
} else {
console.log(items,'inserted')
callback()
}
});
},function(err){
if(err) console.log('error occured')
console.log('all items uploaded');
});