我有一个 azure 的函数,看起来有点像这样:
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);
}
当我在本地运行该函数时,我收到此警告或错误:
Error: Choose either to return a promise or call 'done'. Do not use both in your script.
文档中尚不清楚这应该如何工作
最佳答案
看看 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.
如果不是,请将 function.json
中的 name
属性更改为 $return
。
对于http触发器,
{
"type": "http",
"direction": "out",
"name": "$return"
}
你的函数代码也需要修改。请注意,doStuff()
应该按预期返回有效响应。
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();
}
关于azure - 从异步的 azure 函数返回什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54630039/