Solved

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

TuanDangA
Shopify Partner
17 0 7

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 

Accepted Solution (1)

Nick_Wesselman
Shopify Staff
167 41 63

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

View solution in original post

Replies 2 (2)

Nick_Wesselman
Shopify Staff
167 41 63

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

TuanDangA
Shopify Partner
17 0 7

thank you, I understand now