https://pulumi.com logo
#typescript
Title
# typescript
f

few-rocket-71683

08/03/2023, 10:39 AM
I am stuck with a weird Typescript issue for Pulumi. Any assistance would be much helpful.. I have referred the official TS doc, googled various sources but didnt work out. I'd like to access a value outside its if block. Its always unassigned for me. Am I doing anything wrong?
Copy code
let config = {}
if (env === "qa" || env === "prod" || env === "dev") {
  azure.keyvault
    .getKeyVault({
      name: "name",
      resourceGroupName: "rg-name",
    })
    .then((vault) => {
      return azure.keyvault.getSecret({
        keyVaultId: vault.id,
        name: "secretname",
      });
    })
    .then((secret) => {
      const secret = secret.value;

      config = {
        secret: secret,
      };
    });
}

console.log(config);
This is my code..
f

full-eve-52536

08/03/2023, 1:34 PM
I don't think this would work because of the way Javascript/TypeScript handles asynchronous code. In your example,
console.log(config)
ends up running before the
azure.keyvault
chained promise.
Does the
azure.keyvault
top level Promise return anything? If so, you can assign that return value to a variable and output that. IE
Copy code
let config = {}
if (env === "qa" || env === "prod" || env === "dev") {
  const helloThere = azure.keyvault
    .getKeyVault({
      name: "name",
      resourceGroupName: "rg-name",
    })
    .then((vault) => {
      return azure.keyvault.getSecret({
        keyVaultId: vault.id,
        name: "secretname",
      });
    })
    .then((secret) => {
      const secret = secret.value;

      config = {
        secret: secret,
      };
    });
}

console.log(helloThere.<property>);