title: Handling callbacks with multiple nested functions and for loops?
body: <p>I'm about a week into learning the javascript syntax and had no idea about the whole asynchronous - synchronous situation. I created a function that scrapes names from a page, adds those names to a checklist array, searches each name in the checklist individually and then scrapes the names found in the resulting pages, and so on, until no more names could be found. Kinda like a micro web crawler.</p>
  <p>Anyway that context is beside the issue. My goal is to add the resulting array after all functions and for loops are executed to a txt file. I'm only doing this to make the debugging process a little easier to manage so what happens afterwards doesn't really matter. I'm just trying to understand how to execute these functions in a manner that mimics synchronous processing. I've looked into callbacks, async/await functions, and promises with no luck at successfully implementing them in this script. I've got many nested functions and for loops so my guess is that i'm running into scoping issues.</p>
  <p>This script is a mock up of what i'm actually <strong>scraping</strong>. However the actual script is the same, just changed the variable names and class names being scraped. For the issue at hand, i'm sure this won't matter.</p>
  <pre><code>const fetch = require('node-fetch');
  const cheerio = require('cheerio');
  
  const fruit_names_checklist = []
  const fruit_names = [];
  
  fruitsearch = (callback) =&gt; {
  
      // defines url and search term
  
      const url = &quot;https://www.google.com/search?q=types+of+&quot;
      let fruit_name = &quot;banana&quot;
  
      // gets html of page
  
      findfruits(fruit_name);
      function findfruits(thefruit) {
          return fetch(`${url}${thefruit}`)
          .then(response =&gt; response.text())
          .then(body =&gt; {
  
              // scrapes fruit names and moves them into fruit_names array
  
              const $ = cheerio.load(body);
              $('.llgymd').each( (index, element) =&gt; {
                  const $element = $(element);
                  const names = $element.text();
                  fruit_names[index] = names;
              });
  
              /* for loop that adds each fruit name to fruit_names_checklist 
                 if it doesn't already doesn't already exist in the checklist */
  
              for(j = 0; j &lt; fruit_names.length; j++){
                  if(!fruit_names_checklist.includes(fruit_names[j])){
                       fruit_names_checklist.push(fruit_names[j]);
                       console.log(fruit_names_checklist);
              }}
   
                  /* for loop that goes through each name in the checklist
                     and now searches each name through the same url request. */
  
                  for(let i = 0; i &lt; fruit_names_checklist.length; i++){
  
                      let fruit_name = fruit_names_checklist[i]
                      findfruits_loop(fruit_name);
  
                      function findfruits_loop(thefruit) {
                          return fetch(`${url}${thefruit}`)
                          .then(response =&gt; response.text())
                          .then(body =&gt; {
                              const $ = cheerio.load(body);
                              $('.llgymd').each( (index, element) =&gt; {
                                  const $element = $(element);
                                  const names = $element.text();
                                  fruit_names[index] = names;
                              });
  
                              /* Again adds each fruit name found to fruit_names_checklist 
                                 if it doesn't already exist in the checklist */
  
                              for(j = 0; j &lt; fruit_names.length; j++){
                                  if(!fruit_names_checklist.includes(fruit_names[j])){
                                      fruit_names_checklist.push(fruit_names[j]);
                                  }
                                  console.log(fruit_names_checklist);
                              }
                              // where i've placed the callback
                              callback()
                          });
                      }
                  }
              })
          }
      }
  
      fruitsearch(
      array_to_text = () =&gt; {
          // library that converts array to txt file
          const arrayToTxtFile = require('array-to-txt-file')
          const randomValue_6d = Math.floor(Math.random() * 900000)
          arrayToTxtFile(fruit_names_checklist, `backend_modules/debugging/arrays_to_txt/fruit_names_checklist-${randomValue_6d}.txt`, err =&gt; {
              if(err) {
                  console.error(err)
                  return }
                  console.log('Array successfully wrote to txt file')});
              })
  </code></pre>
  <p>I console logged each fruit name in the for loop that successfully added to the fruit_names_checklist. As you see at the end in the console.log list below, it successfully created a txt file after all loops and functions were completed, however it was called inside of the &quot;i&quot; for loop. So of course created a new txt file for each iteration, which definitely isn't desirable.</p>
  <pre><code>Array(1) [&quot;Plantain&quot;]
  Array(2) [&quot;Plantain&quot;, &quot;Hardy banana&quot;]
  Array(3) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;]
  Array(4) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;]
  Array(5) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;]
  Array(6) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;]
  Array(7) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;]
  Array(8) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;]
  Array(9) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(10) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(11) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(12) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(13) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(14) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(15) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(16) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(17) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(18) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(19) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(20) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(21) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(22) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(23) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(24) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(25) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(26) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(27) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(28) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(29) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(30) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(31) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(32) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(33) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(34) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(35) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(36) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(37) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(38) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(39) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Canceled
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  Array successfully wrote to txt file
  </code></pre>
  <p>This is the closest I've gotten to creating the txt file after all functions and loops are run, if I use callback() at the end of the entire &quot;fruitsearch&quot; function, the txt file is created before any data is added to the checklist. I've tried to callback in almost every place in this main function, either it creates the txt file before any functions are executed or it creates it in a for loop.</p>
  <p>This is the desired outcome</p>
  <pre><code>Array(1) [&quot;Plantain&quot;]
  Array(2) [&quot;Plantain&quot;, &quot;Hardy banana&quot;]
  Array(3) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;]
  Array(4) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;]
  Array(5) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;]
  Array(6) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;]
  Array(7) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;]
  Array(8) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;]
  Array(9) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(10) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(11) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(12) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(13) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(14) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(15) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(16) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(17) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(18) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(19) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(20) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(21) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(22) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(23) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(24) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(25) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(26) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(27) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(28) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(29) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(30) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(31) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(32) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(33) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(34) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(35) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(36) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(37) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(38) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Array(39) [&quot;Plantain&quot;, &quot;Hardy banana&quot;, &quot;Blue Java banana&quot;, &quot;Cavendish banana&quot;, &quot;Musa acuminata&quot;, &quot;Musa balbisiana&quot;, &quot;Red banana&quot;, &quot;Musa velutina&quot;, …]
  Canceled
  Array successfully wrote to txt file
  </code></pre>
  <p>I can't wrap my head around using &quot;callback()&quot; in the context of many nested functions and many nested for loops. Seems that my scoping is off in some way. Looking forward to see what I'm doing wrong here.</p>
  <p>Thanks in advance</p>