Last active
February 12, 2021 14:40
-
-
Save egm0121/cd1886492f93727de17c928fba22c99e to your computer and use it in GitHub Desktop.
simple middleware with sync && async functions support
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
const middy = (middList) => { | |
return (event) => { | |
let middStack = [...middList]; | |
let lastRetValue; | |
const next = () => { | |
const curr = middStack.shift(); | |
if (curr) { | |
const retVal = curr(event, next); | |
lastRetValue = retVal; | |
if (retVal instanceof Promise && middStack.length){ | |
return retVal.then(next); | |
} | |
return retVal; | |
} | |
return lastRetValue; | |
} | |
return next(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment