-1

I have this function

const totalJueces = async(id, idPost) => {
  try{
       const res = await axios(`${process.env.REACT_APP_PJUD_URLBASE}agenda/fullList/${id}/${idPost}`, { 'timeout': 30000 })
       console.log(res.data.listaJuecesAsignadosCausa.length)
  }
  catch(error){
    console.log(error)
  }
}

If I console.log(res.data.listaJuecesAsignadosCausa.length) it's actually giving me the correct response, in this case an integer (I need to know the specific number of arrays in that request)

But If I try to use this function with a return, it returns a Promise pending.

How can I use the correct number to use it as a dynamic parameter in a loop of items?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

1 Answers1

0

Promises will always log pending as long as the results are not resolved yet. You must call .then on the promise, or alternatively, await the promise, to capture the results regardless of the promise state (resolved or still pending).

see Why is my asynchronous function returning Promise { <pending> } instead of a value?

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
  • Instead of answering, you can just vote to close the question as a duplicate of the question you ask them to "see"... – Heretic Monkey Apr 25 '21 at 18:03
  • Oh actually I had already tried that. totalJueces(blockData.crrCausa, blockData.idBloqueAgenda).then(x => console.log(x)) and the console.log it's working fine. But lets say I return the x, it's still a promise. But I'll look more into it, thanks! – leobiscuits Apr 25 '21 at 18:26