title: Chaining async methods in a loop with RxJava
body: <p>I have this code and I want to know if this is possible with RxJava:</p>
  
  <ul>
  <li>Function queries for a User from a server (async)</li>
  <li>The server returns a User JSON object with a list of ID's of associated UserProfile(s)</li>
  <li>Then for each of this ID, it needs to fetch the UserProfile given the ID (async also)</li>
  <li>For each asynchronously fetched UserProfile append it to the User object, below is my pseudo-code.</li>
  <li>I cannot use any blocking code, all request should be async. </li>
  </ul>
  
  <p><strong>Here's the code:</strong></p>
  
  <pre><code>@Override
  public Single&lt;User&gt; retrieve(String entityId) {
      BaasUser baasUser = new BaasUser();
      baasUser.setEntityId(entityId);
      baasUser.setIncludes(Arrays.asList("userProfile"));
      return baasUser.retrieve().map(user -&gt; {
          String username = user.getUsername();
          String dateCreated = user.getDateCreated();
          String dateUpdated = user.getDateUpdated();
  
          List&lt;UserProfile&gt; userProfiles = new LinkedList&lt;&gt;();
  
          BaasLink userProfileLink = user.getFirstLink();
          userProfileLink.getEntities().forEach(stubEntity -&gt; {
              Single&lt;UserProfile&gt; observable = stubEntity.retrieve().map(entity -&gt; {
                  UserProfile userProfile = new UserProfile();
                  userProfile.setEntityId(entity.getStringProperty("entityId"));
                  userProfile.setName(entity.getStringProperty("name"));
                  return userProfile;
              });
              observable.subscribe(userProfile -&gt; {
                  // until all UserProfile is fetched this 'retrieve' "callback" should not return
                  userProfiles.add(userProfile);
              }, error -&gt; {
                 // err
              });
          });
          User user1 = new User();
          user1.setDateCreated(dateCreated);  
          user1.setDateUpdated(dateUpdated);  
          user1.setUsername(username);
          user1.setUserProfiles(userProfiles);
          return user1;
      });
  }
  </code></pre>