title: How to wait for an asynchronous method
body: <p>I need to return the value <code>uId</code>. I am getting the correct value in the first log statement inside the <code>onResponse()</code> function. But when it comes to the return statement it returns <strong>null</strong>. </p>
  
  <p>I think that onResponse() is running on another thread. If so, how can I make the <strong>getNumber()</strong> function wait till the <strong>onResponse()</strong> function finishes execution.(Like thread.join()) </p>
  
  <p>Or is there any other solution?</p>
  
  <p>code :</p>
  
  <pre><code>String uId;
  public String getNumber() {
  
      ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
      Call&lt;TopLead&gt; call = apiInterface.getTopLead();
      call.enqueue(new Callback&lt;TopLead&gt;() {
          @Override
          public void onResponse(Call&lt;TopLead&gt; call, Response&lt;TopLead&gt; response) {
              String phoneNumber;
              TopLead topLead = response.body();
              if (topLead != null) {
                  phoneNumber = topLead.getPhoneNumber().toString();
                  uId = topLead.getUId().toString();
                  //dispaly the correct value of uId
                  Log.i("PHONE NUMBER, UID", phoneNumber +", " + uId);
                  onCallCallback.showToast("Calling " + phoneNumber);
              } else {
                  onCallCallback.showToast("Could not load phone number");
              }
          }
  
          @Override
          public void onFailure(Call&lt;TopLead&gt; call, Throwable t) {
              t.printStackTrace();
          }
      });
      //output: Return uid null
      Log.i("Return"," uid" + uId);
      return uId; 
  </code></pre>