Hi all. Is there a way to call async functions ins...
# general
w
Hi all. Is there a way to call async functions inside pulumi code (Typescript) ? I often find it useful to execute other tasks (write a file, call an api, etc) based on resources created by pulumi. In another runtime this wouldn't be an issue - but many node packages are async only - and I have not been able to use them with pulumi. Anyone had success with this?
b
what problems are you having? i haven't had any issues calling async stuff
b
Input<T>
includes
Promise<T>
. You should be able to just pass through the promise.
w
Yes - you can. You are possibly struggling most with this at top level in your Node modules (or index.ts), where JavaScript does not directly support `async`/`await`. You can work around this by putting your code inside
async function main() { ... }
and then invoking
main()
at top level.
w
Ha. Thanks. Good to know - I guess this top level trick was one of the missing pieces. I'll try again just knowing that it should work fine and see where I get stuck.
Thanks for the help @white-balloon-205 and others - I'm able to use async now 🙂 One remaining question - I'm updating some external api based on a pulumi resource (e.g. in an
output.apply
), this is executed even on previews or when the resource hasn't changed. I presume a better way to do this is to create a Custom Resource? Are there any examples of how to do that - or other recommended patterns?
b
there's a "dynamic provider" examples are in this repo: https://github.com/pulumi/pulumi/blob/master/examples/dynamic-provider/derived-inputs/index.ts i've had problems with it though - it serializes the source code into the stack and because it gets serialized differently on linux and windows it causes unnecessary changes for some of our team
w
ok great, thanks @better-rainbow-14549. I'll take a look a look at that.