node.js - azure nodejs sdk的wait()函数

标签 node.js azure azure-node-sdk

是否有像 python SDK 那样可用于 azure nodejs SDK 的 wait() 函数。 下面是我在 azure 中启动 VM 的 python 代码

async_vm_start = compute_client.virtual_machines.start(group_name, vm_name)
async_vm_start.wait()

所以我的整个程序在这里等待,直到虚拟机完全旋转并继续。 下面是我启动 VM 的 Nodejs 代码

clientCompute.virtualMachines.start(resourceGroupName, vmName, function(err, result) {
    if (err) console.log(err);
    else console.log(result);
})

这里我的程序不会等待,而是继续前进,即使在幕后虚拟机仍在启动过程中。

我尝试过 setTimeout 但它不可靠。那么有什么办法让我的nodejs函数等待VM完全启动。

提前致谢

最佳答案

start() 函数的第三个参数是一个回调函数,将在虚拟机启动后调用。如果您有以下代码:

console.log('I will be logged first!')
clientCompute.virtualMachines.start(resourceGroupName, vmName, function(err, result) {
    console.log('I will be logged after the VM has started!')
})
console.log('I will be logged almost immediately after the first!')

您将看到它们运行的​​顺序。如果虚拟机启动后需要发生任何事情,请在该回调内执行。

当然,如果您有很多事情要做,并且回调内有回调,那么这可能会很痛苦……它只会变得困惑。我不确定您使用的 SDK 版本是什么,但对于较新的版本,您可以使用 async/await:

console.log('I will be logged first!')
await clientCompute.virtualMachines.start(resourceGroupName, vmName)
console.log('I will be logged after the VM has started!')

或者使用 promisify 将基于回调的函数转换为可以等待的 Promise。

关于node.js - azure nodejs sdk的wait()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68767263/

相关文章:

node.js - 如何使用 Node js从mongodb中的3个集合中获取数据?

azure - 如何确保我部署的azure功能正常工作?

azure - 从 Azure 辅助角色获取异常详细信息

azure - 如何从 Nodejs 堆栈上的 Azure Function App 中的 Azure key 保管库中提取 key

node.js - Azure Node 托管文件/文件夹权限

javascript - 导入 Nunjucks 宏?

node.js - 如何在 Node/Postgres 中查询关联模型

node.js - Azure 网站内的 MQTT 代理

c++ - 等同于在 C++ 中重新解释 Node.js fii

c# - 验证 SWT token REST WCF 服务