title: RxJava async while loop
body: <p>I would like to implement the async while loop in Java using RxJava.</p>
  
  <p>More specifically, here is my non-async Java code:</p>
  
  <pre><code>    for (String dataCenter : dataCenters) {
          final Set&lt;Server&gt; serversInDataCenter = getServersInDataCenterSync(dataCenter);
          if (!CollectionUtils.isEmpty(serversInDataCenter)) {
              final Server available = findOneWithSlots(serversInDataCenter);
              if (available != null) {
                  return available;
              }
          }
          // if no available server found for current dataCenter, try next
      }
      return null;
  </code></pre>
  
  <p>What the code above does is that finds an available server from a data center.</p>
  
  <p>Since 90% of the cases, there will be a server available in the first data center that is checked, I don't want to fetch all servers for all data centers in advance.</p>
  
  <p>Now, imagine <code>Set&lt;Server&gt; getServersInDataCenterSync(String dataCenter)</code> method is changed to an async one and it takes a callback instead: <code>void getServersInDataCenter(String dataCenter, AsyncResultHandler&lt;Set&lt;Server&gt;&gt; handler)</code>. That also makes another thing</p>