Skip to content

Instantly share code, notes, and snippets.

@egm0121
Last active February 12, 2021 14:40
Show Gist options
  • Save egm0121/cd1886492f93727de17c928fba22c99e to your computer and use it in GitHub Desktop.
Save egm0121/cd1886492f93727de17c928fba22c99e to your computer and use it in GitHub Desktop.
simple middleware with sync && async functions support
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