Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Choose either to return a promise or call 'done'" in Azure Functions with Koa

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.

like image 240
Kai Avatar asked Nov 05 '25 22:11

Kai


1 Answers

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.

like image 77
Kai Avatar answered Nov 08 '25 14:11

Kai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!