[英]Chaining promise to create ec2 instance
我想以promise方式重写脚本,该脚本以前使用回调来创建ec2实例。 我的逻辑是首先尝试描述securityGroups。 如果不存在,请创建一个,否则继续描述密钥对。 如果密钥对不存在,请创建一个,并将私钥写入文件,否则继续创建实例。
我设法做到了如下,但是我对代码的平坦性不满意。 仍有很多缩进。 而且,我发现我的逻辑迫使我将承诺链嵌套在catch块中,这不是人们在那些教程中通常采用的方式,因此使我怀疑使用承诺的方式和最初的目的。
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个级别的承诺链。
我的第一个问题是我是否能够在describeKeyPairsInstance
catch块之后describeSecurityGroupsInstance
describeKeyPairsInstance Promise放入顶级链describeSecurityGroupsInstance
,因为从逻辑上讲,在与securityGroup进行检查之后,我们应该对KeyPairs进行检查或进行反向检查。(IMO这两个步骤之间的顺序无关紧要,正确我如果我错了)。 基本上,我可以忍受嵌套链来解决securityGroups不存在的问题,因为这是必须要做的,但不是describeKeyPairsInstance
部分。 如果可以实现此目标,我认为writeFileInstance
也可以附加到顶级链,那么问题就可以解决。 因此,问题就变成了如何将承诺从第二级链返回到顶级链。 现在,尽管我认为这不可行,但是如果有解决方案,我将非常感谢。
我的第二个问题是创建ec2实例本身的逻辑。 我是否应该简单地摆脱这两个promise的describeSecurityGroups/KeyPairs
,而是根据createSecurityGroup/KeyPair
的拒绝检查是否存在? 如果是这样,那么无论securityGroup和keyPair是否存在,我都可以连锁承诺。 但是,从全能角度来说,我们不应该首先检查存在吗?
我的第三个问题是关于诺言的一般用法。 将承诺链嵌套在捕获块中是不好的做法吗? 如果是这样,有没有承诺的替代方式是什么?
回答任何问题都会有所帮助。 提前致谢。
如果您不熟悉,JavaScript ES7会引入async / await ,可以解决您正在描述的确切问题。 它本质上是围绕您已经拥有的语法糖,但是绝对看起来不错,而不是先进行.then
然后将它们链接在一起。
通过“反转” 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);
}
});
使用异步/等待(以及反向逻辑),代码看起来更加清晰
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.