Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda