Hi, I have a function with a simple ternary expres...
# typescript
a
Hi, I have a function with a simple ternary expression that doesn’t work and always evals to true. Can anyone tell me where I’m going wrong?
Copy code
const iamId = (): (pulumi.Input<string> | pulumi.Output<string>) => {
            return account.name.apply(name => name.includes('iam')) ? account.id : args.iamAccountId
        }
🤦‍♂️
Copy code
const iamId = (): (pulumi.Input<string> | pulumi.Output<string>) => {
            return account.name.apply(name => name.includes('iam') ? account.id : pulumi.output(args.iamAccountId))
        }
l
Is this the fixed solution? The first snippet would already return
account.id
, but this version looks better. Is it doing what you want now?
a
Hey @little-cartoon-10569, yes it is doing what I want it to do.
However, the return type at this point will only ever be
pulumi.Output<string>
l
Yes that's correct. Since the return value is based on an async value (account.name), it will never be possible to make the return value available synchronously.
Asides: 1. There isn't a need to use
pulumi.Input<string> | pulumi.Output<string>
, since
pulumi.Input<string>
is defined to be
string | pulumi.Output<string> | Promise<string>
. 2. Is there a need to define an anonymous function to get this value? In most cases you should be able to use
const iamId = account.name.apply(name => name.includes('iam') ? account.id : pulumi.output(args.iamAccountId));
💯 1
a
@little-cartoon-10569, you’re absolutely right. Thank you for the tip. As you can probably tell I’m still learning typescript/pulumi having used terraform mainly in the past.