title: Superagent async nested loop http call
body: <p>I'm trying to use superagent to get the link in table in webpage and get value from another table in that link.</p>
  
  <p>So far I have succeed what I want in console.log but having problem trying to return the object with asynchronous call.</p>
  
  <p>The first <code>await request</code> in <code>getData</code> function works fine but another one inside <code>getOrder</code> which also inside cheerio <code>.each</code> is not work as I expected.</p>
  
  <p><em>mention the cheerio here because I not sure what is cause the problem</em></p>
  
  <p><strong>Here what I've done so far</strong></p>
  
  
  
  <pre><code>const request = require('superagent');
  const cheerio = require('cheerio')
  
  server.get('/crawler', (req, res) =&gt; {
    const data = await getData(url)
    console.log('data loaded ')
    res.send(data)
  })
  
  const getData = async (url) =&gt; {
    console.log('before root')
    const result = await request.get(url).set(header)
    console.log('after root')
  
    const $ = cheerio.load(result.text);
  
    let p = Promise.resolve();
    $('table').first().find('tr').each(async (i, row) =&gt; {
      p = p.then(async () =&gt; {
        console.log('order no', row.orderNo)
        let viewOrder = await getOrder(row.url)
        console.log('after getOrder')
  
        // .... build data here 
  
      })
    })
    p.then(() =&gt; {
      // ....
      return data
    })
  }
  
  const getOrder = async (url) =&gt; {
    console.log('before call')
    const result = await request.get(url).set(header)
    // the response return before this line in 1st round of loop 
    console.log('called', url)
  
    const $ = cheerio.load(result.text);
  
    let p = Promise.resolve();
    $('table').first().find('tr').each(async (i, row) =&gt; {
      p = p.then(async () =&gt; {
        // .... build data here
      })
  
    })
    p.then(() =&gt; {
      return data
    })
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>before root
  after root
  order no 1
  before call
  data loaded
  GET /crawler &lt;&lt;&lt;&lt;&lt;&lt;&lt; res.send here
  called ...
  after getOrder
  order no 2
  before call
  called ...
  after getOrder
  before call
  order no 3
  called ...
  after getOrder
  </code></pre>
  
  <p><strong>What it should be</strong></p>
  
  <p>Program should wait for all request is called then return the response</p>