4

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.

Kai
  • 71
  • 1
  • 6

1 Answers1

2

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.

Kai
  • 71
  • 1
  • 6