-1

I've run into some issues with my scoping, I'm new to javascript so my apologies for my inexperience. The issue i'm having is I don't understand why my console.log within the function groupA(thename) curly braces won't allow code to output. In visual studio's it greys the code out. I think it may be due to the return fetch(`${url}${thename}`), because the code won't grey out when I remove that return. Why does the return function do this?

My goal is to correctly chain each function together so the data pushed into birds_array can be returned via the groupA function. How would I go about correctly formatting these functions to allow this to be possible?

const birds_array = []

let groupAname = "Old World sparrow"
groupA(groupAname);

function groupA(thename) {
    return fetch(`${url}${thename}`)
    .then(response => response.text())
    .then(body => {
        const $ = cheerio.load(body);
        $('.llgymd').each( (index, element) => {
            const $element = $(element);
            const names = $element.text();
            groupA_array[index] = names;
        });


        groupA_loop()
        function groupA_loop(){
        for(j = 0; j < groupA_array.length; j++){
            if(!birds_array.includes(groupA_array[j])){
                birds_array.push(groupA_array[j]);
                }
                // code functions here
                console.log(birds_array);
            }
            // code functions here
            console.log(birds_array);
        }
        // code functions here
        console.log(birds_array);
    });
    // code does not function here
    console.log(birds_array);
}
srb633
  • 793
  • 3
  • 8
  • 16
  • 2
    console.log(birds_array); is dead code...this is async funtion. u have to use then to get value – xdeepakv May 01 '20 at 08:10
  • Okay interesting, can you show me an example? – srb633 May 01 '20 at 08:11
  • Does this answer your question? [Understanding JavaScript promise object](https://stackoverflow.com/questions/39458201/understanding-javascript-promise-object) – xdeepakv May 01 '20 at 08:12
  • understand the promise.. – xdeepakv May 01 '20 at 08:12
  • I've looked into promises, async/await and callbacks. I don't understand how to implement it in this situation. Please show me an example. Thank you. – srb633 May 01 '20 at 08:13
  • 1
    It sounds like you already know the solution, as you see that inside the `then` callback the `console.log` statement works. – Bergi May 01 '20 at 08:13
  • I've been learning javascript for about a week. Most of this code was through watching tutorials and trying to understand as I go. I learn by doing, so it would be really helpful if you can be more specific as I don't understand this syntax very well. I'd appreciate it but it's fine if you don't want to help. – srb633 May 01 '20 at 08:16
  • 1
    All the working console.logs are within your return statement so to say, if you follow the {} you'll see that the last console.log is after return. And that is "dead" code because the execution is left the function after the return finished. – Rijad Husic May 01 '20 at 08:18
  • 1
    adde basic example in answer. Please check – xdeepakv May 01 '20 at 08:19

1 Answers1

1

// Create a async function to await any async code. Else u get error.

async function main() {

      let groupAname = "Old World sparrow";
      // Await for the promise to be resolved. // Like pause call
      const birds_array = await groupA(groupAname);
      // Result done, print
      console.log(birds_array)
    }
main()

Sample:

const delayData = (num) => {
  return new Promise((r) => {
    setTimeout(() => {
      r(num);
    }, 1000);
  });
};
async function main() {
  const num = await delayData(1);

  console.log(num); // 1
  const num2 = await delayData(2);

  console.log(num2); // 2
}
main();

// Using then

delayData(1).then(num => {
  console.log(num) // 1
})
delayData(2).then(num => {
  console.log(num) // 2
})
xdeepakv
  • 7,835
  • 2
  • 22
  • 32