Last active
January 19, 2017 09:38
-
-
Save akirattii/fa6859651f5140579069176d054b4f83 to your computer and use it in GitHub Desktop.
node step module example using `parallel()`
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 asyncFn(str, cb) { | |
| setTimeout(function() { | |
| let err = null; | |
| let result = " Hello " + str; | |
| return cb(err, result); // error-first style callback | |
| }, 1000); | |
| } | |
| // Step executes async functions synchronously | |
| Step( | |
| // Step1: | |
| function() { | |
| console.log("step1:"); | |
| asyncFn("hoge", this); | |
| }, | |
| // Step2: | |
| function(err, result) { | |
| console.log("step2:"); | |
| console.log(" err=", err, ", result=", result); | |
| if (err) throw err; | |
| // *** passing 2nd arg of next function | |
| asyncFn("foo", this.parallel()); | |
| // *** passing 3rd arg of next function | |
| (function(_p, cb) { | |
| return cb(null, _p); | |
| })("Hello baa", this.parallel()); | |
| // *** passing 4th arg of next function | |
| asyncFn("baz", this.parallel()); | |
| }, | |
| // Step3: | |
| function(err, result1, result2, result3) { | |
| console.log("step3:"); | |
| if (err) throw err; | |
| console.log(" err=", err, ", result1=", result1, ", result2=", result2,", result3=", result3); | |
| }); | |
| /* | |
| ### Result: | |
| step1: | |
| step2: | |
| err= null , result= Hello hoge | |
| step3: | |
| err= undefined , result1= Hello foo , result2= Hello baa , result3= Hello baz | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment