title: Javascript - Redirect after promise
body: <p>I am trying to implement the behavior of a server call with promises.</p>
  
  <p>What I want to happen is, when the reply from the server is "success" I want to redirect to "success" page else if it is "failure" I want to redirect to "failure" page.</p>
  
  <p>The current situation is that the response is not being waited for, and the browser is redirected <strong>before</strong> I get any response from the server, doesn't matter if it is success or fail. </p>
  
  <p>What am I doing wrong, and how can I solve it?</p>
  
  <p>Here is the relevant part of my code:</p>
  
  <pre><code>var ch_item;
  Payment.postInvoice(dataObj, $rootScope.usertoken).then(function(res){
    if (res.message === "success") {
      setTimeout(function () {
        var payment_details = res.data;
        User.getUserById(res.data.stylist_uuid, res.data.customer_uuid).then(function (stylist) {
          ch_item = TC_Service.getChatCredentials(stylistInfo.uid, $rootScope.userId, $rootScope.usertoken, dataObj.request_uuid);
          openModelAlert("Thank you for your booking!");
          $location.path('/success');
        });
      }, 500);
    } else {
      setTimeout(function () {
        openModal("Sorry, please come back later.");
        $location.path('/failure');
      }, 500);
    }
    $modalInstance.dismiss('cancel');
    $location.path('/default');
  }, function () {
    console.log("Something went wrong with the payment, the response is: "+res.message);
  });
  </code></pre>
  
  <p>Help me out guys :)</p>
  
  <p>Thank you for your time..</p>
  
  <p>V.</p>