1
votes

I have an azure function that looks a bit like this:

module.exports = async (context: Context, req: any) => {
  const canProceed = await new Auth().verify(context, req);

  if (!canProceed) {
    context.res = {
      status: 401,
      body: 'Unauthorised'
    };

    return context.done(undefined, context);
  }

  context.res = doStuff();

  return context.done(undefined, context);
}

When I run the function locally I get this warning or error:

Error: Choose either to return a promise or call 'done'. Do not use both in your script.

It is not clear from the docs how this should work

1

1 Answers

4
votes

Have a look at the doc.

When using the async function declaration or plain JavaScript Promises in version 2.x of the Functions runtime, you do not need to explicitly call the context.done callback to signal that your function has completed. Your function completes when the exported async function/Promise completes.

When exporting an async function, you can also configure an output binding to take the return value. This is recommended if you only have one output binding.

Change the name property to $return in function.json if it's not.

For http trigger,

{
  "type": "http",
  "direction": "out",
  "name": "$return"
}

And your function code needs modification as well. Note that doStuff() is supposed to return a valid response as expected.

module.exports = async (context: Context, req: any) => {
  const canProceed = await new Auth().verify(context, req);

  if (!canProceed) {
    return {
      status: 401,
      body: 'Unauthorised'
    };
  }
  else return doStuff();

}