I have a resource that has a string value that I w...
# typescript
d
I have a resource that has a string value that I want to return. But I don’t know it immediately. How can I return it as a promise-like output that pulumi recognizes?
More importantly how do I define it so that before its assigned pulumi recognizes that it has to wait for a value.
b
inside an
apply
d
This is what I have in my resource:
Copy code
export default class ValidCertificate extends pulumi.ComponentResource {           
    
    private certificateArn: pulumi.Output<string>;                                 
      
            
    constructor(name: string, args: { fqdn: string, zoneId: pulumi.Input<string> }, opts?: pulumi.ComponentResourceOptions) {
       
        //stuff...
        certificateArn = certificate.arn

        //other stuff
    }
}
Is that sufficient?
I have a getter that returns the certificateArn
Wouldn’t this be undefined until its assigned?
b
i’m sorry, not really following the question 😅
d
I have a resource which I’m returning an output that I’m creating. But that output is not immediately known. It comes from a promise. How do I create my own output this way?
b
ah:
Copy code
pulumi.output(myValue)
You might have to resolve the promise first of course so you have a string
d
I’m confused. How does pulumi know that it’s going to get an output?
How does it know that myCertificateResource.certificateArn has a value if initially it’s undefined and only set in the constructor after the promise has been fulfilled?
Copy code
export default class ValidCertificate extends pulumi.ComponentResource {           
    
    private certificateArn: pulumi.Output<string>;                                 
      
            
    constructor(name: string, args: { fqdn: string, zoneId: pulumi.Input<string> }, opts?: pulumi.ComponentResourceOptions) {
       
        //stuff...
        mypromise.then(result => {
          certificateArn = pulumi.output(certificateArn)
        });

        //other stuff
    }
}
Then later:
Copy code
myValidCertificateResource.certificateArn
Should I just make it a regular promise?
l
If the certificate is being created by Pulumi, then it's an output already, just use it. If you're using your own code to create / retrieve it, then put that code in an async function. The value returned from the function is a Promise. Pulumi understands Promises (look at the definition of
Input<>
) and will do the right thing.