我在一个async.parallel调用中有这个aysnc.eachSeries功能。我硬编码了一个错误,这样我就可以通过它,看看它是否符合我的想法。由于某些原因,当我传递一个错误时,它不会在最后一个名为"doneWithSeries“的回调中抛出。
async.eachSeries(jsonDataArr, function iterator(item, callback) {
async.parallel([
function (cb) {
if (item.hasOwnProperty('event.type')) {
var event_type = item['event.type'];
delete item['event.type'];
try {
var json = JSON.stringify(item);
}
catch (err) {
throw err;
}
fs.writeFile('./enriched_data/' + event_type + '.json', json, function (err) {
if (err) {
cb(err);
}
else {
cb(null);
}
});
}
},
function (cb) {
if (item.hasOwnProperty('status_desc')) {
var status_desc = item['status_desc'];
delete item['status_desc'];
try {
var json = JSON.stringify(item);
}
catch (err) {
throw err;
}
fs.writeFile('./enriched_data/' + status_desc + '.json', json, function (err) {
if (err) {
cb(err);
}
else {
cb(null);
}
});
}
}
],
function doneWithParallel(err) {
callback(new Error('throw this baby')); //shouldn't the first incident of error pass the error straight to the doneWithSeries callback below?
})
},
function doneWithSeries(err) {
if (err) {
throw err;
}
else {
console.log('success');
}
});下面是代码的精馏版本,没有任何不必要的内容:
var async = require('async');
async.eachSeries(['1', '2'], function (item, callback) {
async.parallel([
function (cb) {
setTimeout(function () {
cb(null, 'one');
}, 200);
},
function (cb) {
setTimeout(function () {
cb(null, 'two');
}, 100);
}
],
function doneWithParallel(err, results) {
console.log('results', results);
callback(new Error('duh'));
})
},
function doneWithSeries(err) {
if (err)
throw err;
});的确,这很管用。无法理解为什么上面的代码没有,也许可以接受数组可能是空的,即使当我运行代码时成功消息得到了logged...weird。
发布于 2015-06-11 03:16:16
我认为如果你的名单是空的,那就是预期的行为。即使没有输入列表,异步始终会调用最后的回调,而不会出现错误。
https://stackoverflow.com/questions/30770784
复制相似问题