Reputation: 55
I use the async library to manage my asyncronous requests in a web worker using the queue object. But when i run the queue objects kill command it doesn't go into idle state oposing to the documentation that says:
kill: a function that removes the drain callback and empties remaining tasks from the queue forcing it to go idle. Invoke with queue.kill().
I want to stop the queue and kill the web worker when there is a error in one task. But i found out, that kill isn't finished when i call the message that tells my worker manager to kill this worker. Instead it infinitly tells me idle is false.
I use the following code:
var q = async.queue(function(task, callback) {
FileReader.readBlock(task.file, task.offset, task.blockSize)
.then(function(block) {
return sendBlock(task.uuid, block, task.blockNumber);
})
.then(function(result) {
callback(null, result);
}).catch(function(error) {
callback(error);
});
}, 8);
q.drain = function() {
console.log('All Tasks finished successfully.');
};
q.error = function(error, task) {
this.kill();
while(true) {
if(this.idle()) {
self.postMessage({
type: 'error',
msg: 'A task failed. Upload is killed.'
});
break;
}
}
};
Is that a bug in async or is there a mistake on my part?
Upvotes: 0
Views: 678
Reputation: 1688
Double check the version of the async library that you are using. The error
callback was added in version 2.0.
If you are using version 1.5, you will need to handle errors in the callback you pass when adding a task to the queue. Adapted from the docs:
q.push({name: 'foo'}, function (err) {
if (err) {
// handle error here
q.kill();
// ... etc
}
console.log('finished processing foo');
});
Upvotes: 0