I am stuck at resolving an array of promise in Hydrogen
Use case:
I want to show products from multiple collections on the article page
the handles of all those collections are available as a tag of the article
What I am doing:
I am using following code to create an array of promise inside a loader
const collectionHandles = ['handle-one', 'handle-two'];
const topProductsPromises = collectionHandles.map((handle, i) => {
return context.storefront.query(TOPPRODUCTS_QUERY, {
variables: {
collectionHandle: handle,
totalProduct: i+4,
},
});
});
I am returning topProductsPromises using defer
inside the jsx component when try to resolve the array, it is an empty array.
Don’t know why?Is there anybody have similar usecase?
You are returning an “array of promises” while the “defer” does not yet support nested or array promises; it only supports returning promises directly. Support for nested promise data will be added after the singleFetch feature is stable, possibly in Remix v3. You can check it out here: https://www.youtube.com/live/kL1x9ORAjcg.
1 Like
Hello @crpatel
You can use the Below code.
const collectionHandles = ['men', 'women'];
const topProductsPromises = await Promise.all(collectionHandles.map((handle, i) => {
return context.storefront.query(TOPPRODUCTS_QUERY, {
variables: {
collectionHandle: handle,
totalProduct: i+4,
},
});
}));
If the solution presented meets your needs and addresses your query effectively, I encourage you to accept it as the chosen answer. This will acknowledge the support you received and aid fellow community members in identifying reliable and effective solutions for their similar concerns.
Thank you.
Thanks @Huptech-Web
this solution worked, I just used it without await, i want to resolve it in the client
.
and some other info I found related to this from remix roadmap