简体   繁体   English

等待() function 对于 azure nodejs sdk

[英]wait() function for azure nodejs sdk

Is there a wait() function available for azure nodejs SDK just like what python SDK has.是否有 wait() function 可用于 azure nodejs SDK 就像 python SDK 一样。 Below is my python code to start a VM in azure下面是我的 python 代码,用于在 azure 中启动虚拟机

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

So here my whole program waits till the VM completely spins up and proceeds.所以在这里我的整个程序一直等到 VM 完全启动并继续运行。 Below is my nodejs code to start a VM下面是我启动 VM 的 nodejs 代码

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

Here my program don't wait and just proceeds further even though behind the scenes the VM is still in the process of starting.在这里,我的程序不会等待,而是会继续执行,即使在幕后 VM 仍在启动过程中。

I have tried setTimeout but it's not reliable.我试过 setTimeout 但它不可靠。 So is there any way for my nodejs function to wait till the VM completely start.那么有什么办法让我的 nodejs function 等到 VM 完全启动。

Thanks in advance提前致谢

The third argument to the start() function is a callback function which will be called after the VM has been started. start() function 的第三个参数是回调 function,它将在 VM 启动后调用。 If you had the following code:如果你有以下代码:

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!')

you would see the order that they will run.您会看到它们运行的顺序。 If there is anything that needs to happen after the VM has started, do it inside of that callback.如果在 VM 启动需要发生任何事情,请在该回调内执行。

Of course, it can be a pain if you have many things to do in order and you have a callback inside of a callback inside of a callback...it just gets messy.当然,如果你有很多事情要做,而且你在回调中有一个回调,那可能会很痛苦……它只会变得混乱。 I'm not sure what version of the SDK you are using but with newer ones you could use async/await:我不确定您使用的是哪个版本的 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!')

or use promisify to convert callback-based functions into promises which can be awaited.或使用 promisify 将基于回调的函数转换为可以等待的承诺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure function 时间触发器 sdk python - Azure function Time Trigger sdk python azure function 重试场景的单元测试用例 - nodejs - azure function unit test cases for retry scenario - nodejs 无法从 NodeJS 中的 Azure httpTriggered Function 获取响应数据 - Can not get the data in response from Azure httpTriggered Function in NodeJS NodeJs AWS-SDK s3.upload 上传 function 没有按预期抛出错误 - NodeJs AWS-SDK s3.upload upload function not throwing error as expected 错误:在 NodeJS AWS 中找不到模块“aws-sdk” Lambda Function - Error: Cannot find module 'aws-sdk' in NodeJS AWS Lambda Function 如何允许租户 lambda 函数使用 NodeJs SDK 查询 Control Tower ROOT 中的发电机表? - How to allow a Tenant lambda function to query a dynamo table in Control Tower ROOT using NodeJs SDK? 在 Go SDK 等待 AWS Athena 查询执行 - Wait for AWS Athena query execution in Go SDK Azure Function app - Python SDK - 如何阅读服务总线消息正文 - Azure Function app - Python SDK - How to read Service Bus Message body 将 nodejs 和 mongoDB 部署到 azure - deploying nodejs and mongoDB to azure 如何使用 HTTP 触发器解析 Azure Function App 中的多部分表单数据? (节点) - How to parse Multi-part form data in an Azure Function App with HTTP Trigger? (NodeJS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM