Pulumi,如何导出来自异步函数的值? [英] Pulumi, how to export values coming from async function?

查看:167
本文介绍了Pulumi,如何导出来自异步函数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Pulumi项目中,我必须在index.ts文件中调用
const awsIdentity = await aws.getCallerIdentity({ async: true });

In my Pulumi project, in the index.ts file I have to call
const awsIdentity = await aws.getCallerIdentity({ async: true });

因此,出于这个原因,我必须将我的所有代码包装到async函数中. 我的问题是文件末尾的导出变量.

So for this reason I have to wrapp all my code into async function. My problem is with exported variables at the end of the file.

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在执行结束时显示了值).

I want to export these 4 values. I can't understand how, but the code that follows exports( at least, pulumi up shows the values at the end of execution).

const result = go();
export const dnsZoneName = result.then((res) => res.dnsZoneName);

查看

我认为我不能使用 top-level-wait .

什么是明确的解决方案?

What is the clear solution ?

推荐答案

来自您链接的问题,这似乎是我在对上一个问题的答案中的第一个建议应该起作用:为每个问题导出一个承诺价值.根据问题评论,Pulumi似乎可以理解出口的承诺.

From the issue you linked, it seems like my first suggestion in my answer to your previous question should work: Export a promise for each value. Based on the issue comments, it looks like Pulumi understands exported promises.

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,但是您还没有说为什么.

You've said you don't think you can use top-level await, but you haven't said why.

万一只是您在弄清楚如何使用它时遇到麻烦,您可以按照aws.getCallerIdentity和"..."中的内容进行操作.您的代码示例提供了承诺:

In case it's just that you're having trouble figuring out how to use it, you'd do it like this provided aws.getCallerIdentity and whatever's in the "..." of your code example provide 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;

或者如果您需要导出具有属性的对象作为默认导出:

Or if you need to export an object with those as properties as a default export:

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
};

请注意,在两种情况下,代码都不在任何函数内,而是在模块的顶层.

Note that in both cases, the code isn't inside any function, that's at the top-level of your module.

使用Node.js v13和v14(到目前为止),您需要--harmony-top-level-await运行时标志.我的猜测是,它不会在v15(甚至可能只是v14的更高版本)中没有标记.

With Node.js v13 and v14 (so far) you need the --harmony-top-level-await runtime flag. My guess is that it won't be behind a flag in v15 (or possibly even just a later version of v14).

这篇关于Pulumi,如何导出来自异步函数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆