[英]aws-sdk-js E2 createVPC is executed twice while waiting adding a promise
ec2
.createVpc(params, function (err, data) {
if (err) {
console.log(err, err.stack);
// an error occurred
} else {
logger.log.info(
`Deployment VPC ${deployment.name} has been created.`
);
db.saveData(data, deployment.name);
return data;
}
})
.promise()
I was trying to run this code above, which is the most simplest resource in the docs.我试图在上面运行这段代码,这是文档中最简单的资源。 But when I added .promise()
the create operation was triggered twice.但是当我添加.promise()
时,创建操作被触发了两次。 When I remove it, it creates only one instance of VPC.当我删除它时,它只会创建一个 VPC 实例。 But I need to access the information about the created resource in order to save it to the database.但是我需要访问有关已创建资源的信息才能将其保存到数据库中。
I would guess that this happens because you mix two ways of triggering a request - providing a callback and calling a promise.我猜这是因为你混合了两种触发请求的方式——提供回调和调用 promise。 If you want to use promises, you should process the returned data in then()
(or use await
).如果要使用 Promise,则应在then()
中处理返回的数据(或使用await
)。
Example with then()
: then()
示例:
ec2.createVpc(params).promise().then((data) => {
logger.log.info(
`Deployment VPC ${deployment.name} has been created.`
);
db.saveData(data, deployment.name);
return data;
}, (err) => {
console.log(err, err.stack);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.