Manually resolving AJAX return with promise
<p>I tried to grasp my head around promises, and it looked simple. I imagined them as asynchronous vars. One of the problem I needed to resolve was making several AJAX calls which all needed to be resolved successfully, chaining them one after another, aka I need to make them synchronous to get the result. As making ajax call <code>sync: false</code> is deprecated I tried to make AJAX synchronous with promises.</p>

<pre><code>     $.when(
          ajaxCall(param1),
          ajaxCall(param2)
     ).then(
        function(data) {
          console.log(['OK: ',data]);
        }, function() {
          console.log('Error');
        }
     ).fail(function () {
        console.log('Fail');
     });
</code></pre>

<p>And ajax call function is defined as:</p>

<pre><code>ajaxCall: function ($id) {

        $.ajax({

             url: 'some_url'
             type: 'POST',
             dataType: "JSON",
             data: packed,

             success: function (data) {

                 var deferred = $.Deferred();
                 switch (data.code) {
                     case 200:
                         deferred.resolve(data);
                         break;
                     default:
                         deferred.reject(data);
                         break;
                 }

                 return deferred.promise();

             },
             error: function () {
                 $.Deferred().reject();
             }
         });

     }
</code></pre>

<p>I directly made some false ajax calls, but the .fail branch was never executed, and I am not sure what the problem is.
If I change <code>.then</code> to <code>.done</code>, then at least Error callback is executed, but no mater what I tried, I never reached Fail branch.</p>

<p>Solution is probably simple, but looks like I am unable to properly formulate the question to find solution to my problem.</p>

<p>So I have tree questions:</p>

<ol>
<li>Why the <code>.fail</code> is never called, and what I need to change to make it work? </li>
<li>I basically understand difference between <code>.done</code> and <code>.then</code>. Both are called when promise is resolved, only that <code>.then</code> is chainable while done is not (If I'm correct). <code>.done</code> and <code>.fail</code> are usually paired as both have one callback, and <code>.then</code> has two. As I used two callbacks in <code>.done</code> why is second one called, since all callbacks in done are success callbacks?</li>
<li>Why reject doesn't call fail branch?</li>
</ol>

<p>Any help is appreciated.</p>