Skip to content

Instantly share code, notes, and snippets.

@joeyciechanowicz
Last active May 1, 2020 08:51
Show Gist options
  • Save joeyciechanowicz/b746eabd5e155366f75f964deac9f404 to your computer and use it in GitHub Desktop.
Save joeyciechanowicz/b746eabd5e155366f75f964deac9f404 to your computer and use it in GitHub Desktop.
quick testing for a stack of async middlewares
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