Last active
May 1, 2020 08:51
-
-
Save joeyciechanowicz/b746eabd5e155366f75f964deac9f404 to your computer and use it in GitHub Desktop.
quick testing for a stack of async middlewares
This file contains 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
function addField(req, res, next) { | |
res.field = 1; | |
next(); | |
} | |
function incrementField(req, res, next) { | |
res.field++; | |
next(); | |
} | |
function copyField(req, res, next) { | |
res.fieldCopy = res.field; | |
next(); | |
} | |
function callMiddleware(req, res, middleware) { | |
return new Promise((resolve, reject) => { | |
middleware(req, res, err => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(); | |
}); | |
}); | |
} | |
async function callMiddlewares(req, res, middlewares) { | |
for (const middleware of middlewares) { | |
await callMiddleware(req, res, middleware); | |
} | |
} | |
const res1 = {}; | |
callMiddlewares({}, res1, [addField, incrementField, copyField]).then(() => { | |
console.log(res1); | |
}); | |
const res2 = {}; | |
callMiddleware({}, res2, addField).then(() => { | |
console.log(res2); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment