title: How can I use async/await in 'new Promise' block to fetch data from an api?
body: <p>I'm unable to use async/await inside a 'new Promise' block which throws <code>await is only valid in async function</code> error. I don't know if it's even possible but I need to use it or a way to achieve what I want. Here's what I'm trying to do:</p>
  <p>A user types in a query and it goes through a <code>recursive function</code> called <code>doesExist</code>. I have to use a recursive function because the API doesn't always provide data for a query. The recursive function will try 3 times, that is, it will send 3 api requests for the data before returning an error saying 'data couldn't be fetched'. In the function, I return a new promise as I have to make api requests from there.</p>
  <p>To fetch data, I used <code>request</code> before but I want to use <code>axios</code> now with async/await. So how can I use axios with async/await inside a <code>new Promise</code> block?</p>
  <p>This is the code with <code>request</code>:</p>
  <pre><code>router.get('/example', async (req, res) =&gt; {
  
          try{
              const query = 'Superman';
              const data  = await doesExist(query);
      
              if(!data) {
                  console.log('No data');
              }
      
              res.render('../views/example', { data, query });
          }catch(err) {
              console.log(err);
          }
          
  });
      
      
  const doesExist = (query, retries = 0) =&gt; {
  
          const url           = `http://api.example.com/json?fields=${query}`;
          const maxRetries    = 3;
      
          return new Promise(( resolve, reject ) =&gt; {
              
              const retry = () =&gt; {
                  if (retries &lt; maxRetries) {
                      resolve(doesExist(query, retries + 1));
                  } else {
                      reject(`Could not get the data after ${retries} retries.`);
                  }
              };
      
              request(url, function (error, response, body) {
                  if (!error &amp;&amp; response.statusCode === 200) {
                      const data = JSON.parse(body);
                      resolve(data);
                  } else {
                      retry();
                  }
              });
         });
  }; 
  </code></pre>
  <p>And this is what I tried with <code>async/await</code> which throws the error:</p>
  <pre><code>const doesExist = async (query, retries = 0) =&gt; {
  
          const url           = `http://api.example.com/json?fields=${query}`;
          const maxRetries    = 3;
      
          return new Promise(( resolve, reject ) =&gt; {
              
              const retry = () =&gt; {
                  if (retries &lt; maxRetries) {
                      resolve(doesExist(query, retries + 1));
                  } else {
                      reject(`Could not get the data after ${retries} retries.`);
                  }
              };
  
              const data = await axios.get(url);
  
              if(data.statusCode === 200) {
                  resolve(data);
              }else{
                  retry();
              }
       
         });
  }; 
  </code></pre>
  <p>If it's not possible to use async/await inside promise block then please show me how I can achieve it. Thanks</p>