Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise support #8156

Closed
2 tasks
neg3ntropy opened this issue May 22, 2020 · 6 comments
Closed
2 tasks

Promise support #8156

neg3ntropy opened this issue May 22, 2020 · 6 comments
Assignees
Labels
feature-request A feature should be added or improved. guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged.

Comments

@neg3ntropy
Copy link

It's unclear how to use async js operations with constructs and still be able to run cdk commands.

Use Case

I want to add a rule to a security group allowing my current public IP and I need to do a network request to discover it.

Proposed Solution

Maybe constructs that need async operations could override some lifecycle method and return a promise there.

Other

I am new to CDK, sorry if this has been abundantly discussed elsewhere, I could not find it.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@neg3ntropy neg3ntropy added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels May 22, 2020
@pofallon
Copy link
Contributor

I've been churning on this same issue (needing to do async operations in a Construct) and would also be interested in a solution for this (either explicit support, or some existing pattern/guidance). Thanks!

@hoegertn
Copy link
Contributor

You should be able to instantiate your CDK stack inside an immediately invoked async function expression.

(async () => {
	const myIp = await something();
	new MyStack(app, myIp);
})()

@neg3ntropy
Copy link
Author

@hoegertn throwing a promise without anyone looking after it may or may not work depending on the order of things. CDK has to be aware of the moment constructs are finalized.
Furthermore this pattern would require to do all IO essentially in one place then pass parameters from construct to construct, which is not great for encapsulation.

If we are showing code I would hope for something like this:

class MyConstruct extends cdk.Construct {

     protected async onPrepare(): Promise<void> {
          const res = await something();
          new OtherConstruct(this, {res});
     }
}

@SomayaB SomayaB added the guidance Question that needs advice or information. label May 26, 2020
@eladb
Copy link
Contributor

eladb commented May 27, 2020

This is not something we will be able to support with the current programming model of the CDK.

@sam-goodwin
Copy link
Contributor

This is not something we will be able to support with the current programming model of the CDK.

@eladb I understand why we can't support promises in the constructor, but why not in onSynthesize? Why can't the framework that is interpreting the Construct tree be implementing using asynchronous APIs?

@eladb
Copy link
Contributor

eladb commented Nov 14, 2021

We've removed support for onSynthesize() in constructs@10.x and basically moved all responsibility related to synthesis to the domain-specific engine. We've realized this hook is potentially hazardous since it can easily be abused to mutate the tree during synthesis and then we get ourselves into issues related to dependencies and ordering which are very hard to model and get right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

6 participants