在我的Pulumi项目中,在index.ts文件中,我必须调用
const awsIdentity = await aws.getCallerIdentity({ async: true });
因此,我必须将所有代码包装到async函数中。我的问题是文件末尾的导出变量。
async function go() {
...
const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
...
return {
dnsZoneName: DNSZone.name,
BucketID: Bucket.id,
dbHardURL: DBHost.publicDns,
devDbURL: publicDbAddress.fqdn,
};
}我想要export这4个值。我不明白是怎么回事,但是导出之后的代码(至少,pulumi up在执行结束时显示了值)。
const result = go();
export const dnsZoneName = result.then((res) => res.dnsZoneName);看看this
我想我不能用top-level-await。
明确的解决方案是什么?
发布于 2020-06-27 19:13:45
从issue you linked来看,我在answer to your previous question中的第一个建议应该是可行的:为每个值导出一个promise。根据问题的评论,看起来普鲁米理解了出口承诺。
async function go() {
...
const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
...
return {
dnsZoneName: DNSZone.name,
BucketID: Bucket.id,
dbHardURL: DBHost.publicDns,
devDbURL: publicDbAddress.fqdn,
};
}
const goPromise = go();
goPromise.catch(error => {
// Report the error. Note that since we don't chain on this, it doesn't
// prevent the exports below from rejecting (so Pulumi will see the error too,
// which seems best).
});
export const dnsZoneName = goPromise.then(res => res.DNSZone.name);
export const BucketID = goPromise.then(res => res.Bucket.id);
export const dbHardURL = goPromise.then(res => res.DBHost.publicDns);
export const devDbURL = goPromise.then(res => res.publicDbAddress.fqdn);否则:
您说过您不认为可以使用顶级await,但您没有说明原因。
如果只是你在弄清楚如何使用它时遇到了麻烦,你可以像这样做,提供aws.getCallerIdentity和"...“中的任何东西。您的代码示例提供了promises:
const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
// ...
export const dnsZoneName = DNSZone.name;
export const BucketID = Bucket.id;
export const dbHardURL = DBHost.publicDns;
export const devDbURL = publicDbAddress.fqdn;或者,如果您需要将具有这些属性的对象作为默认导出导出:
const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
// ...
export default {
dnsZoneName: DNSZone.name
BucketID: Bucket.id
dbHardURL: DBHost.publicDns
devDbURL: publicDbAddress.fqdn
};请注意,在这两种情况下,代码都不在任何函数中,而是位于模块的顶层。
对于Node.js、v13和v14 (到目前为止),您需要--harmony-top-level-await运行时标志。我的猜测是,它不会在v15 (甚至可能只是v14的更高版本)的标志后面。
发布于 2021-11-30 05:14:51
对于任何读过这篇文章的人来说,异步入口点现在是Pulumi的一等公民,详细介绍Here
你可以有类似这样的代码,Pulumi会自动为你解决所有问题,不需要再做任何导出输出的事情
export async function go() {
...
const awsIdentity = await aws.getCallerIdentity({ async: true });
const accountId = awsIdentity.accountId;
...
return {
dnsZoneName: DNSZone.name,
BucketID: Bucket.id,
dbHardURL: DBHost.publicDns,
devDbURL: publicDbAddress.fqdn,
};
}发布于 2020-06-27 18:47:44
你是在用承诺工作。因此,答案随时都有可能出现。您应该做的是,在调用函数时,使用await来等待函数响应
https://stackoverflow.com/questions/62608754
复制相似问题