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

bored-river-53178

02/03/2020, 12:23 AM
ok. I've found the solution: value = await output.promise()
unfortunately, while stuff like Output.apply() is well documented, the fact that the underlying value of an output could be easily acquired via Output.promise() is kinda hard to find
w

white-balloon-205

02/03/2020, 4:32 AM
To answer the original question -
apply
is the recommended solution. You can pass
apply
a function that returns a
Promise
, so in particular you can pass it an
async
function and use
await
inside it is you are looking for the convenience that provides. It is not recommended to use
.promise()
in general - it’s behavior when there are unknown (not yet known) values is harder to work with, and it does not correctly track dependencies or secretness. It is generally treated as an implementation detail - not part of the documented API. In general, apply with an async callback should give you what you need instead.
b

bored-river-53178

02/03/2020, 9:09 AM
@white-balloon-205 could you please provide an example of replacing
getOutputSync
with
apply
? I am quite new to javascript and promises so I am not sure what you mean I want to have all outputs from a stackReference to be assigned to variables at the start of the program, is that possible?
m

many-garden-84306

02/03/2020, 11:47 PM
One way you could definitely do it is to run a pulumi command as a shell command at the start of your program to fetch the outputs of the other stack --
Copy code
pulumi stack --stack "$(pulumi whoami)/<project-name>/<stack-name>" output --json
w

white-balloon-205

02/04/2020, 12:02 AM
could you please provide an example of replacing
getOutputSync
with
apply
?
Pulumi in Node.js (ike Node.js itself) is fundamentally async, so you cannot do something identical - but you should always be able to express what you need to. I can't tell exactly what code you are trying to write - but here is something that sounds similar that does work:
Copy code
import * as pulumi from "@pulumi/pulumi";
import * as fs from "fs";

const otherStack = new pulumi.StackReference("other", {
    name: "other"
});

otherStack.getOutput("something").apply(async (something) => {
    await fs.promises.writeFile("./out.txt", something);
    console.log("wrote file");
});
b

bored-river-53178

02/04/2020, 12:12 AM
Ah, thank you, it answers the original question, yes. But now I am more interested in the general possibility to assign the underlying value of referenced stack Outputs to variables, like it was possible with
getOutputSync
before the latest changes to nodejs. Right now it seems to me that
await Output.promise()
is the easiest way, and since there should be no issues with not yet known values or dependencies in that specific case (referenced stack) the only disadvantage I see could be related to secrets handling
@many-garden-84306 thanks for the suggestion, it's also an option to call this shell command from pulumi, but I would prefer something more 'native'
m

many-garden-84306

02/04/2020, 12:14 AM
of course
3 Views