我正在尝试在 .then
函数中分配一个变量,但我不知道如何在 svelte store 中执行此操作。我知道这是一个 promise ,需要一些时间来分配。
这是代码。
map 功能
data.products.data = data?.products?.data?.map((item) => {
let category = '';
customPIMListStore.getProductCategory(item.productCategoryId, token).then((data) => {
category = data;
console.log('category inside', category);
});
return {
...item,
category
};
});
获取类别的另一个函数:
getProductCategory: async (id, token) => {
const res = await api.get(`backoffice/v1/category/${id}`, token);
return res?.category?.name;
},
最佳答案
你将不得不等待它
data.products.data = await Promise.all(data?.products?.data?.map(async (item) => {
const category = customPIMListStore.getProductCategory(item.productCategoryId, token)
return {
...item,
category
};
}));
关于javascript - 在映射函数中的 .then 内部分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73333182/