Discussing Shopify Functions development, deployment, and usage in Shopify apps.
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
Solved! Go to the solution
This is an accepted solution.
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.
Nick Wesselman | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
This is an accepted solution.
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.
Nick Wesselman | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
thank you, I understand now