When creating a custom component resource with typ...
# typescript
c
When creating a custom component resource with typescript how should async methods be handled in the constructor? Does it not return until
this.registerOutputs
is called because I can invoke that in the callback? Like this?
Copy code
export class Vote extends pulumi.ComponentResource {
    constructor(
        name: string,
        args: VoteArgs,
        opts?: pulumi.ComponentResourceOptions,
    ) {
        super("svmkit:index:Vote", name, args, opts);
        const parent = this;

        const tag = naming(name);

        const { endpoint, params } = args;

        this.create(endpoint, params)
            .catch((err) => {
                parent.registerOutputs({});
            })
            .then(() => {
                parent.registerOutputs({});
            });
    }
}
Copy code
export async function constructVote(
    name: string,
    inputs: pulumi.Inputs,
    options: pulumi.ComponentResourceOptions,
): Promise<provider.ConstructResult> {
    const signature = await Vote.create(inputs.endpoint, inputs.params);
    const vote = new Vote(name, inputs as VoteArgs, signature, options);

    return {
        urn: vote.urn,
        state: {
            signature: vote.signature,
        },
    };
}
I’m calling the async function in the construct function and then updating the call signature of the component.
l
Resources hold async things, they're not async themselves. You should register the outputs synchronously. Store the asynchronously-created thing in an Output.
c
@little-cartoon-10569 then the first strategy I posted using registerOutputs is the the correct approach?
l
No. You need to register the outputs before you return from the constrictor. The code above might register it afterwards
c
Ok. Then where do I call the async code? If I registerOutputs syncrounously how does the async value make it to the outputs?
Oh or do I return the promise on the output and pulumi calls it? That likely it.
l
Outputs are promises. You store the output that wraps the value or object you want in your resource.
c
https://www.pulumi.com/docs/concepts/resources/dynamic-providers/ dynamic resource provider seems more appropriate for my use case.
l
Likely. Dynamic providers have functions that get run at deployment time, inside the Pulumi engine. Resource constructors are run earlier, during state creation.