是否有像 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/