queue async continue on error
darul75 opened this issue · 2 comments
darul75 commented
hi,
first great job I love it.
would it be possible to get results even when one or several errors have occured during process. it would be great
thanks,
julien
var q = queue(1);
q.defer(fnAsync1, {});
q.defer(fnAsync2; {}); // throw error
q.defer(fnAsync3, {});
q.awaitAll(function(error, results) {
// results from 1 and 3
res.json(results);
});
mbostock commented
The recommended way of doing this is to invoke the callback without an error, and then pass whatever you want into the results. For example, you could wrap a task that calls back with an error with something that instead returns a null result:
queue(1)
.defer(ignoreError, taskThatSometimesFails)
.defer(ignoreError, taskThatSometimesFails)
.awaitAll(function(error, results) {
if (error) throw error; // never happens
console.log(results); // e.g., [0.23068457026965916, undefined]
});
function ignoreError(task, callback) {
task(function(error, result) {
return callback(null, result); // ignore error
});
}
function taskThatSometimesFails(callback) {
setTimeout(function() {
var x = Math.random();
if (x > .5) return callback(new Error("failed"));
callback(null, x);
}, 250);
}
darul75 commented
perfect, thank you for use case and code