title: How to wait for callback to finish before continuing the loop?
body: <p>I'm trying to add <code>restMedia</code> to <code>restaurantMediaList</code> in <code>onResponse</code> within a for loop. However, when the loop is finished, <code>restaurantMediaList</code> is null. How can I fix this in such a way that it waits for <code>onResponse</code> to be finished first before proceeding with the next iteration?</p>
  <pre><code>public void getImages(List&lt;Restaurant&gt; restaurantList, OnImageReceivedCallback callback){
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(&quot;https://example.com&quot;)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
          WordpressAPICall wordpressAPICall = retrofit.create(WordpressAPICall.class);
          
          for(int i = 0; i &lt; restaurantList.size(); i++){
              String featuredMediaURL = restaurantList.get(i).get_links().getFeaturedMedia().get(0).getHref();
              featuredMediaURL.substring(featuredMediaURL.indexOf(&quot;v2&quot;)+1);
              Call&lt;RestaurantMedia&gt; restMediaCall = wordpressAPICall.getImage(featuredMediaURL);
  
              restMediaCall.enqueue(new Callback&lt;RestaurantMedia&gt;() {
                  @Override
                  public void onResponse(Call&lt;RestaurantMedia&gt; call, Response&lt;RestaurantMedia&gt; response) {
                      RestaurantMedia restMedia = response.body();
                      restaurantMediaList.add(restMedia);
                      //callback.onRestaurantListReceived(restaurantModels, restMedia);
                  }
  
                  @Override
                  public void onFailure(Call&lt;RestaurantMedia&gt; call, Throwable t) {
                      Log.d(&quot;Fail to get media&quot;, t.toString());
                  }
              });
          }
          
          callback.onImageReceived(restaurantMediaList);
          
      }
  </code></pre>