Created
August 10, 2020 08:59
-
-
Save rolldone/0af2c72b8d5495bad470aa125982becf to your computer and use it in GitHub Desktop.
Javascript basic recursive for middleware
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 recursive = function(index,done,middlewares,props=null){ | |
| console.log(index,'length',middlewares.length) | |
| if(index == middlewares.length){ | |
| return done(); | |
| } | |
| var next = index + 1; | |
| console.log('next',next); | |
| console.log('index',index); | |
| return middlewares[index](props,done,recursive.bind(this,next,done,middlewares)) | |
| } | |
| var middlewares = [function(props,done,next){ | |
| console.log('props',props); | |
| console.log('step 1'); | |
| next({ | |
| ...props, | |
| b : 2 | |
| }); | |
| },function(props,done,next){ | |
| console.log('props',props); | |
| console.log('step 2'); | |
| setTimeout(function(){ | |
| next(); | |
| },5000) | |
| },async function(props,done,next){ | |
| console.log('step 3'); | |
| var test = function(){ | |
| return new Promise(function(resolve){ | |
| setTimeout(function(){ | |
| resolve(); | |
| },10000) | |
| }) | |
| } | |
| await test(); | |
| next(); | |
| },function(props,done,next){ | |
| console.log('step 4'); | |
| next(); | |
| }] | |
| recursive(0,function(){ | |
| console.log('done') | |
| },middlewares,{ | |
| a : 1 | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment