简体   繁体   English

aws-sdk-js E2 createVPC 在等待添加 promise 时执行了两次

[英]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.

相关问题 将aws-sdk-js与CognitoSync服务一起使用时出现InvalidSignatureException - InvalidSignatureException while using aws-sdk-js with CognitoSync service 如何使用 aws-sdk-js 列出日期范围内的对象? - How to list objects in a date range with aws-sdk-js? 使用AWS-SDK-JS访问AWS ELB服务时配置CORS - Configure CORS when accessing AWS ELB service using AWS-SDK-JS 如何将 aws-sdk-js 捆绑到无服务器框架优化包中? - How do you bundle aws-sdk-js into a Serverless Framework optimized package? 使用 AWS-SDK-JS 使用普通 Javascript 将大文件作为流上传到 s3 - Upload large files as a stream to s3 with Plain Javascript using AWS-SDK-JS 使用AWS-SDK-JS通过CloudFront分发进行S3分段上传 - S3 Multipart upload via cloudfront distribution with aws-sdk-js aws-sdk-js ReferenceError:您正在尝试在 Jest 环境被拆除后“导入”文件 - aws-sdk-js ReferenceError: You are trying to `import` a file after the Jest environment has been torn down 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url aws-sdk-js DynamoDB引发错误:Request.VALIDATE_REGION的配置中缺少凭据 - aws-sdk-js DynamoDB throwing Error: Missing credentials in config at Request.VALIDATE_REGION Node JS + AWS Promise 触发两次 - Node JS + AWS Promise Triggered Twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM