Skip to content

Instantly share code, notes, and snippets.

@goFrendiAsgard
Created June 24, 2018 03:01
Show Gist options
  • Save goFrendiAsgard/8c318f320817240dd1dc7152c9d666bd to your computer and use it in GitHub Desktop.
Save goFrendiAsgard/8c318f320817240dd1dc7152c9d666bd to your computer and use it in GitHub Desktop.
Using promise and async in Koa
const Koa = require('koa');
const app = new Koa();
function middleWare1 (ctx, next) { // This is a function that return a promise
return new Promise((resolve, reject) => {
ctx.body = 'Hello';
next().then(resolve).catch(reject);
})
}
async function middleWare2 (ctx, next) { // This is an async function
ctx.body += 'World';
await next();
return true;
}
app.use(middleWare1);
app.use(middleWare2);
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment