I'm trying to get koa working with azure functions. The "hello world" app is is already working via koa but while running the dev server azure is throwing an Error:
Choose either to return a promise or call 'done'. Do not use both in your script.
Using Node Version 10.14.1
The code is quite simple, as you can see I never call context.done() explicitly. Could koa somehow call this function? Removing all promises is not an option because of the nature of koa.
When I try calling done there will be another Error message: Error: 'done' has already been called. Please check your script for extraneous calls to 'done'.
const Koa = require('koa')
const app = new Koa()
async function createServer(app, context, req){
app.use(async function(ctx) {
ctx.body = 'Hello World';
})
return app.callback()(req, context.res)
}
module.exports = async function (context, req) {
return await createServer(app, context, req)
}
The app is working correctly but I asume its ill advice to ignore the error message.
Ok I figured it out. It feels like an hack but its working.
const Koa = require('koa')
const app = new Koa()
async function createServer(app, context, req){
app.use(async function(ctx) {
ctx.body = 'Hello World';
})
//remove done fn from context obj so koa can not call it
context.done = () => {}
return app.callback()(req, context.res)
}
module.exports = async function (context, req) {
return createServer(app, context, req)
}
Trick is to reasign context.done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With