title: How do I send my response in a API Route in next js when my data is in a .then promise
body: <p>I am using next js with axios, and I have a .then promise inside a .then. I want to respond to the api request with the data I grabbed but if I put the res in the .then it is unreachable, but I don't know how to get the data out of the .then. Normally with async await I return the value and create a variable to the function but this does not seem to work with .then.</p>
  <p>here is my code</p>
  <pre><code>let data;
  
    axios
      .post(
        &quot;https://accounts.spotify.com/api/token&quot;,
        serialize({
          grant_type: &quot;client_credentials&quot;,
        }),
        {
          headers: {
            Authorization: &quot;Basic &quot; + auth,
          },
        }
      )
      .then((res) =&gt; {
        axios
          .get(
            `https://api.spotify.com/v1/search?q=${name}&amp;type=episode&amp;market=US`,
            {
              headers: {
                Authorization: `Bearer ${res.data.access_token}`,
                &quot;Content-Type&quot;: &quot;application/json&quot;,
              },
            }
          )
          .then(function (response) {
            // handle success
            console.log(response.data.episodes);
            data = response.data.episodes;
          })
          .catch(function (error) {
            // handle error
            console.log(error);
          })
          .then(function () {
            // always executed
          });
      })
      .catch((err) =&gt; {
        console.log(err);
      });
  
    res.status(200).json({ data: data });
  </code></pre>
  <p>Any help would be greatly appreciated thanks</p>