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

124
Visualizações
Javascript Async/Await inside promise

I have methods in a class as below. The await in callback doesn't work. If there is duplicate item, available_quantity inside Api.get should be updated for next item but same quantity is displayed. I think i have used Async/Await properly but still it doesnt work. Please help me solve it.

async checkItems(callback){
    for(const items of (this.all_items)) {  
        let newItem = items.product_id.id;  //item id
        let added_quantity =items.quantity; //total quantity to buy
        await Api.get('materials/get/'+newItem).then(async (data) => { //gets item data from inventory
            let available_quantity = data.quantity; //get total quantity available in inventory
            if (added_quantity <= available_quantity){ //if total quantity available in inventory is greater than buying quantity

                await callback([newItem,(available_quantity - added_quantity)]); 
               //all i want to do is wait for this callback to complete before moving to next item in for loop.

            }
            else{
                items.quantity = 0; //if total quantity available in inventory is less than buying quantity set the buying quantity to 0
            }
            
        });
            
    }
},
updateInventory(itemData){
   return Api.post('/materials/update', {
        id: itemData[0],
        quantity: itemData[1],
    }).catch(() => {});    
},

await this.checkItems(this.updateInventory);
about 4 years ago · Juan Pablo Isaza
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