how can I make my product-discount extension to run asynchronously?

Hi everyone, I’m developing a product-discount extension that can change cart item prices based on values from an API, so I have to make the code run asynchronously (to use await with the fetch command) but It seems to me that the code only run synchronously (the exported function got immediately invoked in the test file without await, and as you all might know, if you do that then a promise is returned, not the output I want). So I’m asking if anyone can help me with this here, how can I make it run asynchronously? Please help, its quite urgent

Hi @TuanDangA – You may be misunderstanding how functions work / when they are invoked. They cannot execute async and do not have access to fetch. The only effects functions can have is via the JSON they output, they cannot have side-effects. See how functions work, functions lifecycle, and limitations.

Your use case may be better accomplished by importing data into a metafield and/or a client-side call which updates a cart attribute. Functions can then read that metafield or attribute.

1 Like

thank you, I understand now