title: How to make recursive promise calls?
body: <p>I am working with an API that gives me data with a limit per request (here 25). Therefore, I have to recursively make promises with fetch. However, while most of my logic works, when I try to return from the function it will return an empty array. The image below will make it more clear.</p>
  <pre><code>const url = (conf_name) =&gt; {
    return (
      &quot;https://api.elsevier.com/content/search/scopus?view=COMPLETE&amp;cursor=*&amp;query=CONFNAME(&quot; +
      conf_name +
      &quot;)%20AND%20DOCTYPE(cp)%20AND%20SRCTYPE(p)&amp;field=dc:creator,dc:title,dc:description,affilname,affiliation-city,affiliation-country,authkeywords,prism:doi,prism:doi,prism:coverDate&quot;
    );
  };
  
  const savePapers = (json, conf_name) =&gt; {
    let temp = new Array();
    for (let i = 0; i &lt; json[&quot;search-results&quot;][&quot;entry&quot;].length; i++) {
      temp[i] = {
        title: json[&quot;search-results&quot;][&quot;entry&quot;][i][&quot;dc:title&quot;],
        author: json[&quot;search-results&quot;][&quot;entry&quot;][i][&quot;dc:creator&quot;],
        publication_date: json[&quot;search-results&quot;][&quot;entry&quot;][i][&quot;prism:coverDate&quot;],
        doi: json[&quot;search-results&quot;][&quot;entry&quot;][i][&quot;prism:doi&quot;],
        abstract: json[&quot;search-results&quot;][&quot;entry&quot;][i][&quot;dc:description&quot;],
        author_keywords: json[&quot;search-results&quot;][&quot;entry&quot;][i][&quot;authkeywords&quot;],
        proceeding: conf_name,
      };
    }
    return temp;
  };
  
  async function getPapers(final, url, conf_name) {
    let total_amount_of_papers;
    let next;
    let position = 2;
  
    try {
      let response = await fetch(url, options);
      let json = await response.json();
  
      total_amount_of_papers = json[&quot;search-results&quot;][&quot;opensearch:totalResults&quot;];
  
      if (json[&quot;search-results&quot;][&quot;link&quot;][position][&quot;@ref&quot;] == &quot;prev&quot;)
        next = json[&quot;search-results&quot;][&quot;link&quot;][position + 1][&quot;@href&quot;];
      next = json[&quot;search-results&quot;][&quot;link&quot;][position][&quot;@href&quot;];
  
      final = final.concat(savePapers(json, conf_name));
      if (final.length === 50) {
          console.log(&quot;hey&quot;,final.length);
          return final;
      }
      await getPapers(final, next, conf_name);
    } catch (error) {
      console.log(error);
    }
  }
  
  const createNewConf = async (conferences) =&gt; {
    let final = new Array();
    try {
      var temp = new Conference({
        acronym: conferences.acronym,
        name: conferences.fullname,
        area: conferences.area,
        subarea: conferences.subarea,
        location: conferences.location,
        url: conferences.url,
        description: conferences.description,
        papers: await getPapers(final, url(conferences.acronym),conferences.acronym),
      });
      console.log(temp.papers.length);
    } catch (error) {
      console.log(error);
    }
    return temp;
  };
  
  describe(&quot;Saving records&quot;, function () {
    it(&quot;Saved records to the database&quot;, async function (done) {
      var conferences = [];
      try {
        for (var i = 0; i &lt;= 1; i++) {
          conferences[i] = await createNewConf(json_conferences[i]);
          conferences[i].save().then(function () {
            assert(conferences[i].isNew === True);
            done();
          });
        }
        mongoose.connection.close();
      } catch (error) {
        console.log(error);
      }
    });
  });
  
  </code></pre>
  <p>Below you can see the length of my final array after passing the if to stop fetching more. and the second number is what I receive in the initial call</p>
  <p><a href="https://i.stack.imgur.com/4aVes.png" rel="nofollow noreferrer">Console</a></p>
  <p>Maybe anyone has an idea of the undefined behavior that occurs during return.</p>
  <p>Your help is much appreciated.</p>