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

168
Visualizações
react native cant get data from function

i want to get the product stock and use it on detail but i have a problem

i have a fucntion like this.

async function getStock(productId, erp_code) {
    let itemsStock;
    await fetch(`${GET_STOCK}${productId}`, requestOptions)
      .then(response => response.json())
      .then(result => itemsStock = result[erp_code][0].LotQTY)
      .catch(error => console.log('error', error));
    console.log(itemsStock);   i cant console.log the data 
    return itemsStock;
  }

but in return i get a promise but that's not what i want.

i mean i get the data i need in function but i cant use it out of function

thats kinda annoying

Promise {_U: 0, _V: 0, _W: null, _X: null}

how can i fix this?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Assuming that you are getting the response in the expected format, you need to return the value instead of assigning it.

async function getStock(productId, erp_code) {
    const itemsStock = await fetch(`${GET_STOCK}${productId}`, requestOptions)
      .then(response => response.json())
      .then(result => result[erp_code][0].LotQTY)
      .catch(error => console.log('error', error));
    console.log(itemsStock);
    return itemsStock;
  }

Also, you can just use the async-await approach instead of .then and .catch chain:

async function getStock(productId, erp_code) {
    try {
        const response = await fetch(`${GET_STOCK}${productId}`, requestOptions);
        const result = await response.json();
        const itemsStock = result[erp_code][0].LotQTY;
        console.log(itemsStock);
        return itemsStock;
    } catch (error) {
        console.log({ error });
    }
}
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