首页 > 解决方案 > 在 Azure 函数中调用无等待函数

问题描述

可以在 Azure Function 中调用异步函数而不等待该函数吗?

尽管我必须在这里简化代码,但这实际上对我有用。getData() 在 runInBackground() 之前终止。几秒钟后,runInBackground 完成。

但是,它在 MS Azure 文档中实现等待,但不确定是否涵盖了这种情况。鉴于 runInBackground 的用例,这是可以接受的吗?这个实现可能会出现什么问题?

处理程序天蓝色函数

export const getData: AzureFunction = async function (
  context: Context,
  req: HttpRequest,
): Promise<void> {
  try {
    // really need to wait for this
    const data = await service.getData(...);

    // no need to wait, no await
    runInBackground();

    context.res = handleResp(data);
  } catch (e) {
    context.res = handleErr(e);
  }
};

一个旨在单独运行的功能(有点像一劳永逸)。无需知道结果。

export const runInBackground = async () => {
  const url = `https://externalapi/call`;

  try {
     await axios.post(url, {});
  } catch (error) {
     console.log(error.response);
  }
};

编辑:在 getData() 成功返回响应后,runInBackground() 进程是否有可能被终止/删除?我实际上对此进行了测试,没有问题——它继续进行。我只是想知道这是一种成功还是失败。如果有这方面的文件。

标签: node.jsazure-functions

解决方案


推荐阅读