Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
Assign variable inside .then in a map function

I'm trying to assign a variable inside a .then function but I don't know how to do that in svelte store. I know that its a promise and takes some time to be assigned.

Here is the code.

The map function

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
                };
            });

The other function to get the category:

getProductCategory: async (id, token) => {
        const res = await api.get(`backoffice/v1/category/${id}`, token);
        return res?.category?.name;
    },
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You will have to await for it

data.products.data = await Promise.all(data?.products?.data?.map(async (item) => {
  const category = customPIMListStore.getProductCategory(item.productCategoryId, token)
  return {
    ...item,
    category
  };
}));
about 4 years ago · Santiago Trujillo Relatório

0

Since you have asynchrony, you'll need to change your code to

data.products.data = await Promise.all(data?.products?.data?.map(async (item) => {
    const category = await customPIMListStore.getProductCategory(item.productCategoryId, token);
    return {
        ...item,
        category
    };
}));

or

Promise.all(data?.products?.data?.map(async (item) => {
    const category = await customPIMListStore.getProductCategory(item.productCategoryId, token);
    return {
        ...item,
        category
    };
})).then(result => data.products.data = result);
about 4 years ago · Santiago Trujillo Relatório

0

You will have to do you own map implementation. Since the map function is not an async function.

productsData.forEach((item, i) => {
    let category = '';
    customPIMListStore.getProductCategory(item.productCategoryId, token).then((data) => {
        category = data;
        console.log('category inside', category);

        productsData[i] = {
            ...item,
            category
        }
    });
});

EDIT: Convert for loop to forEach loop for security reasons. pointed by Abdel

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda