https://pulumi.com logo
#general
Title
# general
m

microscopic-afternoon-22702

09/12/2023, 3:52 PM
QQ, is there any reason I should not set variables in an apply block that were defined outside of the apply block?
Copy code
let foundVPC = false;

  let vpcId = getVpcsOutput({ filters }).apply((list: GetVpcsResult) => {
    if (list.ids.length > 0) {
      foundJumpVPC = true;
      return vpc;
    } else {
      <http://log.info|log.info>("No VPC found. Skipping...");
      return undefined;
    }
  });

  if(!foundJumpVPC) {
    <http://log.info|log.info>("No VPC found. Skipping...");
    return;
  }
The reason I need to do this is because I can't do something like:
if(!vpcId)
because
vpcId
is of type
Output<string | undefined>
and this condition
if(!vpcId)
will always evaluate to true.
d

dry-keyboard-94795

09/12/2023, 4:15 PM
The
apply
won't run until after the whole file has run. There should be a
getVpcs
that can be used for this though, provided
filters
doesn't need to be an Output
m

microscopic-afternoon-22702

09/12/2023, 4:15 PM
The
apply
won't run until after the whole file has run.
Ah so not a good idea.
The only issue is, I find that the async code doesn't play well in TS constructors.
d

dry-keyboard-94795

09/12/2023, 4:41 PM
Could Promise.resolve work here?
m

microscopic-afternoon-22702

09/12/2023, 4:41 PM
is that blocking?
Oh no, I don't think that works 😞 I don't resolve the promise, something internally will resolve it when
getVpcs
completes.