Created
April 26, 2019 18:24
-
-
Save diamondap/10be9ca856c9afce493954b81537e319 to your computer and use it in GitHub Desktop.
How to stop JavaScript async.queue on error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code shows how to stop a JavaScript async.queue after | |
// the first error. | |
const async = require('async'); | |
// Create a queue that calls the write function (defined below). | |
// Concurrency is set to 1 to guarantee sequential operation. | |
let q = async.queue(write, 1); | |
// The drain function is called when all tasks in the queue are complete. | |
q.drain = () => { console.log('Done'); } | |
// Set an error handler for the queue. | |
q.error = errHandler; | |
// This is the definition of the queue's error handling function. | |
// When a task is complete, it must call its done() callback to tell | |
// the queue it's finished. There's more on this below, but note in | |
// this code that we only kill the queue if the err param is non-empty. | |
// | |
// Note the optional call to q.drain(). Calling q.kill() detaches the | |
// drain event, so if you don't call it explicitly, it won't fire. | |
// | |
function errHandler(err, task) { | |
if (err) { | |
console.log(`Queue Error on task ${task}: ${err}`) | |
q.drain(); // Fire the drain event (optional) | |
q.pause(); // Pause, so no additional items are processed | |
q.kill(); // Empty the queue and don't allow any more additions | |
} | |
} | |
// This is the function that the queue will call for every | |
// task we pass into it. A task can be any arbitrary data structure | |
// we want. The queue will automatically pass a second parameter | |
// to our write function, which is a callback to execute when | |
// write() completes its internal work. The done function takes two | |
// params, err and task. Param err is an optional error object or | |
// string. Param task is the data that was pushed into the queue | |
// (the first param to our write function below). Internally, the done | |
// function will call whatever function we assigned to queue.error above. | |
// | |
// In an async.queue worker function like the one below, calling done() | |
// with no params tells async.queue that processing for this task is | |
// complete, and the queue can proceed with the next task. | |
// | |
// Calling done() with one or more arguments tells async.queue that | |
// something went wrong and it should call the error handler. Note that | |
// unless the error handler explicitly stops the queue (as ours does | |
// above), the queue will run the error handler and then continue | |
// processing all remaining tasks. | |
// | |
function write(number, done) { | |
if (number == 4) { | |
done("Number cannot be 4", number); | |
return; // or else you'll call done() twice | |
} | |
console.log(number); | |
done(); | |
} | |
// This function pushes items into the queue. | |
// For each item we push in, the queue will call | |
// the write() function, passing the item as the | |
// first parameter and a "done" callback as the | |
// second parameter. | |
// | |
function qtest() { | |
for (let i=0; i < 10; i++) { | |
q.push(i); | |
} | |
} | |
// Now run this, and you'll see that processing stops | |
// after the fourth item. Comment out the calls to | |
// q.pause() and q.kill() to see how that changes the | |
// behavior. | |
// | |
qtest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment