Created
January 19, 2017 09:55
-
-
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
This file contains hidden or 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 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