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

fancy-fall-81083

09/19/2018, 7:49 PM
Hi everyone, is there any working example for @stocky-spoon-28903’s hint regarding async/await nodejs calls? I am trying to get it running with azure resource groups (
const resourceGroup = await azure.core.getResourceGroup …
) but am stuck in reading about the typescript Promise type and brain compiling to javascript. Any hints would be highly appreciated. As I am new to async/await that might be my main problem. @stocky-spoon-28903’s example seems to be just the core and I am just not getting how to do my async/await call so that nodejs does not fail with an
ReferenceError: resourceGroup is not defined
when using
resourceGroup.name
later in the code.
w

white-balloon-205

09/19/2018, 8:08 PM
The following should work:
Copy code
import * as azure from "@pulumi/azure";

async function main(): Promise<string> {
    const resourceGroup = await azure.core.getResourceGroup({
        name: "appservice-rg24afd37d",
    });

    return resourceGroup.location;
}

export const location = main();
Note that
getResourceGroup
does not appear to return the
name
, but you also don't really need it because you can only get the resource group by name anyway, so you could just use that name where needed instead of calling
getResourceGroup
. You only need that function if you want to e.g. figure out what location the resource group is in.
Here's another example:
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure";

async function main(): Promise<pulumi.Output<string>> {
    // Get an Azure Resource Group
    const resourceGroupName = "appservice-rg24afd37d";
    const resourceGroup = await azure.core.getResourceGroup({ name: resourceGroupName });

    // Create an Azure resource (Storage Account)
    const account = new azure.storage.Account("storage", {
        resourceGroupName: resourceGroupName,
        location: resourceGroup.location,
        accountTier: "Standard",
        accountReplicationType: "LRS",
    });

    return account.primaryConnectionString;
}

export const connectionString = main()
f

fancy-fall-81083

09/19/2018, 8:18 PM
Hi @white-balloon-205, thanks a lot. However, that looks like typescript and, sorry for the confusion, I have no idea about typescript. Would you mind to send me a js example, that would be great! The first short example would be mighty sufficient.
w

white-balloon-205

09/19/2018, 8:23 PM
How about this?
Copy code
const azure = require("@pulumi/azure")

async function main() {
    const resourceGroup = await azure.core.getResourceGroup({
        name: "appservice-rg24afd37d",
    });

    return resourceGroup.location;
}

exports.location = main();
f

fancy-fall-81083

09/19/2018, 8:53 PM
Thanks a lot and thanks a lot for the js lessons as well 😉. I tried to figure out how to return the whole resourceGroup object at once (so that I can access its values by using resourceGroup.location) but I gave up and just repeated your function multiple times for now. Will try to think about how to do that tomorrow. Javascript really is a beast, no wonder you are using typescript ...
3 Views