How to convert Pulumi Output to TypeScript Promise?
TypeScriptIn Pulumi, the
Output
type represents a value that will only be known at deployment time. You cannot directly convert anOutput<T>
to aPromise<T>
but Pulumi does provide theapply
function to interact with the value inside Output.However, if you have a scenario where you need to convert the output into a promise, you can use
promise()
method to achieve this. Here's an example of usage wherebucket
is a s3.Bucket object:import * as aws from "@pulumi/aws"; const bucket = new aws.s3.Bucket("my-bucket"); // Convert pulumi.Output to a Promise const bucketNamePromise = bucket.id.promise(); bucketNamePromise.then(bucketName => { console.log(`Bucket name is: ${bucketName}`); });
In this code,
bucket.id
returns apulumi.Output
, which we can convert to aPromise
with the.promise()
method. ThePromise
will resolve with the value of theOutput
at the time thePromise
is created.Keep in mind though, you should only convert Outputs to Promises if necessary as it defeats the goal of Pulumi's programming model which is designed to not know the values until deployment time. For example, you may tempt to use promise when you need to run a for loop on the Output value, but in Pulumi, you could achieve it by the use of
Output.apply
function.