OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Javascript get object from an async recursive function return [duplicate]

  • Thread starter Thread starter 360Bits
  • Start date Start date
3

360Bits

Guest
Im doing a form validation where a function calls another async function to fetch data, im trying to receive the data and work with it in the first function, i striped everything and simplified the request to debug it:

  1. Calling the Validation script:

Code:
function lfg_validation() {

    returnData = lfg ();
    console.log(returnData) //logs undefined to the console
    return returnData; //returns the undefined

}
  1. this is calling another function lfg () in which it is calling the async script that fetch's the data:

Code:
function lfg () {

    lfgData = async_lfg();

    lfgData.then(resolve => {
        
        console.log(resolve); //logs the object properly to the console
        
        return values;
    });
  1. async function is logging the data to the console and returning it to the calling function properly

Code:
async function async_lfg () {

    const poke = await  fetch ("https://pokeapi.co/api/v2/pokemon");
    const pokemon = await poke.json();

    console.log(pokemon); //logs the object properly to the console

    return pokemon; //returns the object properly

}
  1. console is logging first undefined to the console and then logs the object two times to the console, i would expect it does log the object 2 times then gets the return log from the initial function call.

tried to resolve this by using .then but it didnt change the result. Where is my error here?

<p>Im doing a form validation where a function calls another async function to fetch data, im trying to receive the data and work with it in the first function, i striped everything and simplified the request to debug it:</p>
<ol>
<li>Calling the Validation script:</li>
</ol>
<pre><code>function lfg_validation() {

returnData = lfg ();
console.log(returnData) //logs undefined to the console
return returnData; //returns the undefined

}
</code></pre>
<ol start="2">
<li>this is calling another function lfg () in which it is calling the async script that fetch's the data:</li>
</ol>
<pre><code>function lfg () {

lfgData = async_lfg();

lfgData.then(resolve => {

console.log(resolve); //logs the object properly to the console

return values;
});

</code></pre>
<ol start="3">
<li>async function is logging the data to the console and returning it to the calling function properly</li>
</ol>
<pre><code>async function async_lfg () {

const poke = await fetch ("https://pokeapi.co/api/v2/pokemon");
const pokemon = await poke.json();

console.log(pokemon); //logs the object properly to the console

return pokemon; //returns the object properly

}
</code></pre>
<ol start="4">
<li>console is logging first undefined to the console and then logs the object two times to the console, i would expect it does log the object 2 times then gets the return log from the initial function call.</li>
</ol>
<p>tried to resolve this by using .then but it didnt change the result. Where is my error here?</p>
 
Top