Skip to content

Instantly share code, notes, and snippets.

@jagoda
Last active December 27, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagoda/7335927 to your computer and use it in GitHub Desktop.
Save jagoda/7335927 to your computer and use it in GitHub Desktop.
Demonstrate non-intuitive `async.waterfall` behavior.
var async = require("async");
function asyncExample (callback) {
console.log("asynchronous error");
async.waterfall(
[
function (next) {
console.log("throwing error");
next(new Error("Fail!"));
},
function (next) {
console.log("after error");
next();
}
],
function (error) {
console.log("final handler");
console.log(error);
callback();
}
);
}
function syncExample () {
console.log("synchronous error");
async.waterfall(
[
function (next) {
console.log("throwing error");
// Synchronous errors bubble up the stack and interrupt execution (meaning that `next` is
// never called).
throw new Error("Fail!");
next();
},
function (next) {
console.log("after error");
next();
}
],
function (error) {
// This never executes because the default exception handler will terminate execution if there
// is an unhandled exception.
console.log("final handler");
console.log(error);
}
);
}
asyncExample(syncExample);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment