Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
reaccionar nativo no puede obtener datos de la función

quiero obtener el stock del producto y usarlo en detalle pero tengo un problema

Tengo una función como esta.

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

pero a cambio recibo una promesa pero eso no es lo que quiero.

quiero decir que obtengo los datos que necesito en función pero no puedo usarlos fuera de función

eso es un poco molesto

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

¿Cómo puedo arreglar esto?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Suponiendo que obtiene la respuesta en el formato esperado, debe devolver el valor en lugar de asignarlo.

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

Además, puede usar el enfoque async-await en lugar de .then y .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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!