title: Node.js + Cheerio : Request inside a loop
body: <p>I'm using cheerio, request and Node.js.</p>
  
  <p>When I run the script below, it outputs names in a wrong order. I believe that it's caused by asynchronous nature of it, how can I make it work in the "right" order? Do I need to use a sync package or is there a way to change it in a way so it'll work in a sync way?</p>
  
  <pre><code>app.get('/returned', function (req, res) {
      for (var y = 0; y &lt; 10; y++) {
          var url = "http://example.com" + y + "/person.html";
          request(url, function (err, resp, body) {
              $ = cheerio.load(body);
              var links = $('#container');
              var name = links.find('span[itemprop="name"]').html(); // name
              if (name == null) {
                  console.log("returned null");
              } else {
                  console.log(name);
              }
  
          });
      }
  });
  </code></pre>