Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created January 19, 2017 09:55
Show Gist options
  • Save akirattii/01f1bf06f284a49fd1e110a7caeb7ade to your computer and use it in GitHub Desktop.
Save akirattii/01f1bf06f284a49fd1e110a7caeb7ade to your computer and use it in GitHub Desktop.
node step module example without error throwing to avoid any troubles related to it
var Step = require("step");
function genRandomError() {
let i = Math.floor(Math.random() * 2) + 1;
if (i === 1) {
return Error("Error occured!");
}
return "Success";
}
// Step without throwing error.
// This comes up with using each function's `result` the 2nd arg
// as Error Object on case that error occured.
Step(
// Step1:
function() {
console.log("step1:");
return genRandomError();
},
// Step2:
function(err, result) {
console.log("step2:");
console.log(" err=", err);
if (result instanceof Error) return result;
return result;
},
// Step3:
function(err, result) {
console.log("step3:");
if (result instanceof Error) {
return console.error("result=", result);
}
return console.log(" err=", err, ", result=", result);
});
/*
### Result:
step1:
step2:
err= undefined
step3:
err= undefined , result= Success
or
step1:
step2:
err= undefined
step3:
result= Error: Error occured!
at Error (native)
at genRandomError
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment