Will I get a Promise once use context.done()?

Viewed 151

I've followed all guidelines from MS writing Azure Function. Somehow have to use .done() else client (Postman) won't see any returns:

module.exports = async function(context)
{
  ...
  const result = await aPromise().catch((bad)=>
    {
    context.res = {status: 401, body: "bad"};
    context.done();  // needed so client can get the above res
    });
}

Does it mean the return is not a Promise?

1 Answers

If your function is synchronous it doesn't return a Promise, so you must pass the context object, as calling context.done is required for correct use.

// You should include context, other arguments are optional

module.exports = function(context, yourTrigger, yourInput) {
    // your function code
    context.done();
};

enter image description here

If your function is async function. You don’t need to call the context.text. While your async function declaration of the Functions runtime, you do not need to explicitly call the context.done callback to signal that your function has been completed.

It is applicable from function version 2.x. If you are using function version 1.x you need to call the context.done callback.

module.exports = async function (context) {
    context.log(‘your log');
};

enter image description here

Note:

Explicitly calling done is unnecessary when writing an async function the done callback is called implicitly.

Refer here for more information

Related