Pulumi, how to export values coming from async function?

和自甴很熟 提交于 2020-06-29 03:35:28

问题


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

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

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

look at this

I think I can't use top-level-await.

What is the clear solution ?


回答1:


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

Otherwise:

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

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.

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).




回答2:


You are working with promises. Therefore, the answer can come at any time. What you should do is that when you call the function you do it with await to wait for the function response



来源:https://stackoverflow.com/questions/62608754/pulumi-how-to-export-values-coming-from-async-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!