javascript - Node async and exception handling -
i'm using async waterfall. when 1 of functions calls callback(err), custom async callback called. inside of there throw error, hoping caught in try block around async, that's not happening.
try { async.waterfall([function1, function2], myasynccallback); } catch(err) { console.log("this code never executed."); } var function1 = function() { ... //some error occurs: callback(new error(errmsg), errmsg); ... } var function2 = function() { ... } function myasynccallback(err, result) { console.log("this code gets executed."); if (err) { console.log("this code gets executed too."); throw new error("i want error caught @ top around catch around async.waterfall()"); } }
https://runkit.com/imjosh/async-try-catch/2.0.0
var async = require('async'); try { async.waterfall([function1, function2], myasynccallback); } catch(err) { errorhandler(err); } function function1(callback) { console.log('in fn1') callback(null,'fn1'); } function function2(fn1, callback) { console.log('in fn2') callback(null, fn1 + 'fn2'); } function myasynccallback(err, result) { if (err) { console.error('there error: ' + err); return; } //an error occurs. gets caught outer try block //var foo = outer; //oops, outer not defined. throws error //same scenario inside async function //can't/won't caught outer try block settimeout(function(){ try{ //need try here var foo = inner; //oops, inner not defined. throws error } catch(err) { errorhandler(err); } }, 1000); console.log('result was: ' + result); } function errorhandler(err){ //make error handler function can called either catch console.log('caught error: ' + err); }
Comments
Post a Comment