javascript - “异步”Azure Function App 未按预期等待

标签 javascript node.js azure async-await azure-functions

我正在尝试使用适用于 Node 的 Azure 存储 SDK 在表格存储中创建表(如果该表不存在)。

以下代码是有效的,并且返回 200 响应,尽管没有响应内容。但是,该表已按预期创建。

经过调查,我可以看到 Azure Function 应用正在记录以下内容 -

Creating table 'Test'.
[warn] Warning: Unexpected call to 'log' on the context object after function execution has completed. Please check for asynchronous calls that are not awaited or calls to 'done' made before function execution completes.

所以看来,虽然createTableInTableStore按预期工作,但我的函数的 async/await 部分却没有。我怀疑我做错了什么,但从外观上看,我在需要时正确实现了 await

如何让函数等待 createTableInTableStore 方法完成后再继续?

示例代码

请注意,需要 azure-storage npm 软件包 (npm install azure-storage)。

module.exports = async function (context) {
    var azure_storage = require('azure-storage');
    var table_service = azure_storage.createTableService(process.env["AzureWebJobsStorage"]);
    var create_table_result = await createTableInTableStore(context, table_service, "Test");
    return {
        res: create_table_result
    };
};

async function createTableInTableStore(context, table_service, table_name) {
    context.log("Creating table '"+table_name+"'.");
    return await table_service.createTableIfNotExists(table_name, function(error, result, response) {
        if (!error && result) {
            context.log.info("[Info] Table created successfully.")
        } else if (!error && !result) {
            context.log.info("[Info] Table already exists.")
        }
        if (error) {
            context.log.error("[Error] An unexpected error occurred.")
            context.log.error(" -----> " + response)
        }
    });
}

最佳答案

你可以返回一个它会起作用的 promise 。并在使用await时使用try catch来处理错误。

module.exports = async function (context) {
    try{
    var azure_storage = require('azure-storage');
    var table_service = azure_storage.createTableService(process.env["AzureWebJobsStorage"]);
    var create_table_result = await createTableInTableStore(context, table_service, "Test");
      return {
          res: create_table_result
      };
    }catch(err){
    //handle errr
    console.log(err);
     }
};

function createTableInTableStore(context, table_service, table_name) {
    context.log("Creating table '"+table_name+"'.");
    return new Promise((resolve, reject) => {
      table_service.createTableIfNotExists(table_name, function(error, result, response) {
        if (!error && result) {
            context.log.info("[Info] Table created successfully.")
            resolve(result)
        } else if (!error && !result) {
            context.log.info("[Info] Table already exists.")
            resolve(response)
        }
        if (error) {
            context.log.error("[Error] An unexpected error occurred.")
            context.log.error(" -----> " + response)
            reject(error)
        }
    });
});
}

关于javascript - “异步”Azure Function App 未按预期等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944356/

相关文章:

c# - 如何从代码隐藏的更新面板中找到客户端元素?

javascript - palm mojo webos 中的滑动效果

javascript - 如何使用 Moment JS 获取时间

javascript - node.js 和 mysql 如果数据库中有 1,我如何获得 2?

node.js - 使用 NPM 在 Windows 7 Box 上安装 MongoDB 时出错

node.js - 我们如何计算两个坐标之间的距离。阿兰戈数据库

azure - 一个 Azure WebRole 中的多个站点

javascript - 尝试获取 <option> 的计数,因为它在 React 应用程序中被过滤掉了

azure - 我是否能够将 IIS 虚拟目录映射到云中的 Azure CDN?

azure - 无法在 Microsoft Azure 中部署以太坊 PoA 模板