title: How to do repeated requests until one succeeds without blocking in node?
body: <p>I have a function that takes a parameter and a callback. It's supposed to do a request to a remote API and get some info based on the parameter. When it gets the info, it needs to send it to the callback. Now, the remote API sometimes fails to provide. I need my function to keep trying until it manages to do it and <strong>then</strong> call the callback with the correct data.</p>
  
  <p>Currently, I have the below code inside the function but I think that stuff like while (<code>!done</code>); isn't proper node code.</p>
  
  <pre><code>var history = {};
  while (true) {
      var done = false;
      var retry = true;
      var req = https.request(options, function(res) {
          var acc = "";
          res.on("data", function(msg) {
              acc += msg.toString("utf-8");
          });
          res.on("end", function() {
              done = true;
              history = JSON.parse(acc);
              if (history.success) {
                  retry = false;
              }
          });
      });
      req.end();
      while (!done);
      if (!retry) break;
  }
  callback(history);
  </code></pre>
  
  <p>How do I do it the right way?</p>