Created
June 24, 2018 03:01
-
-
Save goFrendiAsgard/8c318f320817240dd1dc7152c9d666bd to your computer and use it in GitHub Desktop.
Using promise and async in Koa
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 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