title: How to wait for Rest API to respond in Protractor
body: <p>I am using following function to call API in my Protractor test, and sometimes  API takes time to respond. </p>
  
  <pre><code>var request = require( "superagent" );
  var PostUrl = browser.baseUrl + 'rest/1.0/dev/users';
  var CreateTenantUrl = browser.baseUrl + 'rest/1.0/tenant';
  
  exports.CreateTenant = function(body){  
   var data = '{' + body + '}';       
   request.post(CreateTenantUrl).set('Content-Type', 'application/json').set('serviceSharedSecret', 'sharedsecret').send(data).end(function(err,res){
          if(err){
              console.log("CreateTenant post error=  ", err )
          } else{
              console.log("CreateTenant post response = ", res.status)
          }
          expect(res.status).toEqual(200)
       });     
  };
  
  exports.CreateUsers = function(body){
    var data = '{' +body + '}';
     request.post( PostUrl ).set('Content-Type', 'application/json').send(data).end(function(err,res){
   if(err){
          console.log("CreateUsers post error= ", err )
      } else{
          console.log("CreateUsers post response = ", res.status)
      }
  expect(res.status).toEqual(202)
   });            
  };
  </code></pre>
  
  <p>Call these functions in test script:</p>
  
  <pre><code>Common.CreateTenant('"tid": "1","long_name": "test tenant"');
  Common.CreateUsers('"userName": "test1", "tenantKey": "1", "password": "Test1", "userID": "1"');
  </code></pre>
  
  <p>is there any way to put wait for each API call to complete and then execute the next one?</p>