【问题标题】:Pulumi, how to export values coming from async function?Pulumi,如何导出来自异步函数的值?
【发布时间】:2020-10-17 21:10:53
【问题描述】:

在我的 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 这四个值。 我不明白怎么做,但是导出之后的代码(至少,pulumi up 显示了执行结束时的值)。

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

this

我想我不能使用top-level-await

什么是明确的解决方案?

【问题讨论】:

  • “我想我不能使用 top-level-await。” 为什么不呢?是否有一些 Pulumi 问题?

标签: javascript export pulumi


【解决方案1】:

issue you linked 看来,我在answer to your previous question 中的第一个建议应该可行:为每个值导出一个promise。根据问题 cmets,看起来 Pulumi 理解导出的承诺。

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 并且代码示例的“...”中的任何内容都提供承诺:

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 的更高版本)。

【讨论】:

  • You've said you don't think you can use top-level await, but you haven't said why. 我不确定如何将 NodeJS 参数传递给 Pulumi,使用我当前的设置会出错。我找到了this,它仍然处于打开状态。
  • @TigranSahakyan - 抱歉,我看不出这与顶级await...有什么关系?
  • 我的意思是,我正在使用 pulumi up 运行脚本,但我不知道如何传递 NodeJS 参数。
【解决方案2】:

对于任何看到这篇文章的人来说,异步入口点现在是 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,
  };
}

【讨论】:

    【解决方案3】:

    您正在使用 Promise。因此,答案随时可能到来。你应该做的是,当你调用函数时,你用await 来等待函数响应

    【讨论】:

      猜你喜欢
      • 2022-06-27
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2022-01-21
      相关资源
      最近更新 更多