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

wide-judge-30323

10/17/2019, 4:34 AM
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

better-rainbow-14549

10/17/2019, 9:10 AM
what problems are you having? i haven't had any issues calling async stuff
b

boundless-monkey-50243

10/17/2019, 4:01 PM
Input<T>
includes
Promise<T>
. You should be able to just pass through the promise.
w

white-balloon-205

10/17/2019, 4:57 PM
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

wide-judge-30323

10/18/2019, 3:44 AM
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

better-rainbow-14549

10/18/2019, 10:16 AM
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

wide-judge-30323

10/21/2019, 1:01 AM
ok great, thanks @better-rainbow-14549. I'll take a look a look at that.