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

salmon-account-74572

06/22/2020, 8:35 PM
Bit of a TypeScript newbie here w/ a question on changes necessary to account for "Remove synchronous invokes" in Pulumi 2.x (described at https://www.pulumi.com/docs/get-started/install/migrating-2.0/). I was using
aws.getAvailabilityZones
and using the results to a) create an array of strings to capture the AZ names and b) use the
length
function on said array of strings to determine how many AZs were in the region. I thought that wrapping the
aws.getAvailabilityZones
in
pulumi.output
and then using
apply
would get me back to working, but it appears not. Code is threaded below.
Here's the original TypeScript code I was using with Pulumi 1.x:
Copy code
const rawAzInfo = aws.getAvailabilityZones({
    state: "available",
});
let azNames: Array<string> = rawAzInfo.names;
let numberOfAZs: number = azNames.length;
Here's what I was trying to use w/ Pulumi 2.x:
Copy code
const rawAzInfo = pulumi.output(aws.getAvailabilityZones({
    state: "available",
}));
let azNames = rawAzInfo.apply(rawAzInfo => rawAzInfo.names);
let numberOfAZs = azNames.length;
TS complains that I can't define
azNames
as
Array<string>
because of type mismatch. If I remove the type definition, then I can't define
numberOfAZs
as a
number
due to type mismatch, and then that breaks some numerical operations later in the code.
As a relative newcomer to TypeScript, I'm a bit lost on how I can resolve this. Suggestions?
f

faint-table-42725

06/22/2020, 8:54 PM
Since you’ll end up using the number later on, I would treat it as a promise (instead of wrapping in output). Then, either write your code w/in the
then
of the promise or use https://www.pulumi.com/docs/intro/languages/javascript/#entrypoint to use top-level
await
s

salmon-account-74572

06/22/2020, 9:11 PM
Thanks. Can you suggest some reading that might help me better understand working with promises? I've tried a couple variations of using
then
to no avail, so I think I need to spend some time understanding the concepts a bit better.
f

faint-table-42725

06/22/2020, 10:02 PM
I don’t have a “go to” resource for that. I’ve heard good things about https://www.executeprogram.com/ — and their concurrency lesson seems to be among the free lessons you can try.
👍🏻 1