Last active
December 27, 2015 13:49
-
-
Save jagoda/7335927 to your computer and use it in GitHub Desktop.
Demonstrate non-intuitive `async.waterfall` behavior.
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
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