Conditional async validation function We have the following validation function:

    var result = scope.CurrentUser.Surname.length &gt; 0;
    var d = $q.defer();

    if (result) {
        apiService.UniqueEmailCheck(scope.CurrentUser.PersonId, scope.CurrentUser.Email)
            .success(function (response) {
                if (response) {
                        apiService.GetSelectetExistent(scope.CurrentUser.FirstName, scope.CurrentUser.Surname, scope.CurrentUser.DOB)
                        .success(function (response) {
                                    $scope.SelectExistent = response;
                                    d.resolve($scope.SelectExistent.length &gt; 0);
                        });

            }
            else {
                d.resolve(false);
            }
        })
}
else
    d.resolve(false);

return d.promise;


If the incoming result is true, we have to check one or two async conditions.

It works as expected but we are not sure if we can do better. Any suggestions?
Consider having your APIs return a promise instead. By the looks of it, success indicates that it's a Q promise which means it should be able to do then. This means we can throw away the "outer deferred" and use the promise directly.

Both requests don't necessarily need to be in sequence. The second API call doesn't seem to require the first one other than the fact that it checks for uniqueness and bails out if not unique. You can fire both in parallel, with a minor tradeoff that it will fire 2 in all cases. But then, 2 in parallel vs 2 in sequence. Time-wise, I'd prefer the former.

If you have native promises available or are able to polyfill it, then use native instead. Also, instead of resolving to a boolean, use the promise state as your boolean. Resolve or reject accordingly.

Your code can then be simplified to:

var result = scope.CurrentUser.Surname.length &gt; 0;

var uniquenessRequest = apiService.UniqueEmailCheck(scope.CurrentUser.PersonId, scope.CurrentUser.Email);

var existenceRequest = apiService.GetSelectetExistent(scope.CurrentUser.FirstName, scope.CurrentUser.Surname, scope.CurrentUser.DOB).then( response =&gt; {
  $scope.SelectExistent = response;
  return response;
});

return !result ? Promise.reject() : Promise.all([
  uniquenessRequest,
  existenceRequest
]).then(results =&gt; {
  var pass = response[0] &amp;&amp; response[1].length &gt; 0;
  return Promise[ pass ? 'resolve' : 'reject' ]();
});