【问题标题】:Chaining promise to create ec2 instance链接承诺以创建 ec2 实例
【发布时间】:2019-08-25 05:11:23
【问题描述】:

我想以以前使用回调创建 ec2 实例的承诺方式重写脚本。我的逻辑是首先尝试描述安全组。如果不存在,则创建一个,否则继续描述密钥对。如果密钥对不存在,则创建一个并将私钥写入文件,否则继续创建实例。

不知何故,我设法做到了如下,但我对代码的平坦度不满意。仍然有很多缩进。此外,我发现我的逻辑迫使我将链式 Promise 嵌套在 catch 块中,这不是那些教程中人们通常这样做的方式,从而使我怀疑使用 Promise 的方式和最初目的。

var describeSecurityGroupsInstance = ec2.describeSecurityGroups(securityGroups).promise()
//1st level chain
describeSecurityGroupsInstance.then(function(data){
    console.log("SecurityGroups Already Exist")
}).catch(function(err){
    console.log("Unable to describe SecurityGroup", err)
    var createSecurityGroupInstance = ec2.createSecurityGroup(securityGroup).promise()
    //2nd level chain
    createSecurityGroupInstance.then(function(data){
        console.log("SecurityGroup test created") 
        var describeKeyPairsInstance = ec2.describeKeyPairs(keyPairs).promise()
        return describeKeyPairsInstance
    }).then(function(data){
        console.log("KeyPair Already Exist, make sure you have private key locally to proceed")
    }).catch(function(err){
        console.log("Unable to describe KeyPairs", err)
        var createKeyPairInstance = ec2.createKeyPair(keyPair).promise()
        //3rd level chain
        createKeyPairInstance.then(function(data){
            console.log("KeyPair test created")
            const writeFileInstance = util.promisify(fs.writeFile)
            privateKey=data.KeyMaterial
            return writeFileInstance('test.pem',privateKey)
        }).then(function(data){
            console.log("keypair content write to file")
            var instancePromise = ec2.runInstances(instanceParams).promise()
            return instancePromise
        }).then(function(data){
            console.log("instance just created")
            console.log(data)
        }).catch(function(err){
            console.log("Unable to create KeyPair or unable to write to file or create instance", err, err.stack)
        })
    })
})

所以现在,我有 3 级承诺链。

我的第一个问题是我能否在describeSecurityGroupsInstance 的捕获块之后将describeKeyPairsInstance 承诺放入顶级链中,因为逻辑上在检查安全组之后,我们应该检查 KeyPairs 或相反。(IMO 这两者之间的顺序步骤不重要,如果我错了,请纠正我)。基本上我可以忍受嵌套链来解决 securityGroups 的不存在,因为它是必须的,但不是describeKeyPairsInstance 部分。如果这个目标可以实现,我认为writeFileInstance 也可以附加到顶级链上,那么问题就解决了。所以问题变成了如何将承诺从二级链返回到顶级链。现在虽然我觉得不可行,但是如果有解决方案我真的很感激。

我的第二个问题是创建 ec2 实例本身的逻辑。我应该简单地摆脱describeSecurityGroups/KeyPairs这两个promise,而是根据createSecurityGroup/KeyPairpromise的拒绝检查存在吗?如果是这样,那么无论 securityGroup 和 keyPair 是否存在,我都可以链接 promise。但是幂等的说,我们不应该先检查存在吗?

我的第三个问题是关于 Promise 的一般用法。将承诺链嵌套在 catch 块中是不好的做法吗?如果是这样,有和没有承诺的替代方法是什么?

回答任何问题都会有所帮助。提前致谢。

【问题讨论】:

  • 事实上,您将 function(whatever) 缩进到 .then 和 .error 的新行中,缩进的代码超出了它的需要——除此之外,我无法立即看到不同的方式编写该代码,(除了将一些代码重构为函数,或者可能使用 async/await)
  • @JaromandaX 去掉不必要的缩进确实让它看起来更好。但主要问题仍然存在
  • 什么大问题?如果代码按预期工作,则没有大问题
  • @OscarZhang,编写代码的潜在问题 - ec2.createSecurityGroup() 的失败将被“无法描述密钥对”处理程序捕获。也许是故意的,也许不是。无论哪种方式,捕获或捕获/重新抛出该错误以证明您已经考虑过它是一个好主意。

标签: javascript node.js amazon-web-services amazon-ec2 promise


【解决方案1】:

JavaScript ES7 引入了 async/await 如果您不熟悉它,它可以解决您所描述的确切问题。它本质上是您已有的语法糖,但它肯定看起来更好,而不是使用 .then 并将它们链接在一起。

【讨论】:

  • 你知道什么会使这个答案更好吗?如果您展示了 async/await 如何帮助解决问题中的代码,因为,我认为它不会像您想象的那样有帮助
【解决方案2】:

通过“反转”ec2.describeSecurityGroups 和 ec2.describeKeyPairs 的成功/失败逻辑,您可以“扁平化”这段代码

const writeFileInstance = util.promisify(fs.writeFile);
const describeSecurityGroupsInstance () => ec2.describeSecurityGroups(securityGroups).promise()
    .then(() => { throw 'SecurityGroups Already Exist'}, err => err);
const describeKeyPairsInstance = () => ec2.describeKeyPairs(keyPairs).promise()
    .then(() => { throw 'KeyPair Already Exist, make sure you have private key locally to proceed'}, err => err);

describeSecurityGroupsInstance()
.then(res => {
    console.log("Unable to describe SecurityGroup", res);
    return ec2.createSecurityGroup(securityGroup).promise();
})
.then(describeKeyPairsInstance)
.then(res => {
    console.log("Unable to describe KeyPairs", res);
    return ec2.createKeyPair(keyPair).promise();
})
.then(data => {
    console.log("KeyPair test created");
    privateKey = data.KeyMaterial;
    return writeFileInstance('test.pem',privateKey);
})
.then(data => {
    console.log("keypair content write to file")
    return ec2.runInstances(instanceParams).promise();
})
.then(data => {
    console.log("instance just created");
    console.log(data);
})
.catch(err => {
    if (typeof err === 'string') {
        console.log(err);
    } else {
        console.log(err, err.stack);
    }
});

使用 async/await(连同反转逻辑)代码看起来更干净

const writeFileInstance = util.promisify(fs.writeFile);
const describeSecurityGroupsInstance () => ec2.describeSecurityGroups(securityGroups).promise()
    .then(() => { throw 'SecurityGroups Already Exist'}, err => err);
const describeKeyPairsInstance = () => ec2.describeKeyPairs(keyPairs).promise()
    .then(() => { throw 'KeyPair Already Exist, make sure you have private key locally to proceed'}, err => err);

try {
    const sec = await describeSecurityGroupsInstance();
    console.log("Unable to describe SecurityGroup", sec);
    await ec2.createSecurityGroup(securityGroup).promise();
    const kpi = await describeKeyPairsInstance();
    console.log("Unable to describe KeyPairs", kpi);
    const kp = await ec2.createKeyPair(keyPair).promise();
    console.log("KeyPair test created");
    privateKey = kp.KeyMaterial;
    await writeFileInstance('test.pem',privateKey);
    console.log("keypair content write to file");
    const data = await ec2.runInstances(instanceParams).promise();
    console.log("instance just created");
    console.log(data);
} catch(err) {
    if (typeof err === 'string') {
        console.log(err);
    } else {
        console.log(err, err.stack);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-05
    • 2013-09-15
    • 2020-12-05
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    相关资源
    最近更新 更多