How to make an ajax call for each element of an array of objects, using promises?
<p>I have this code:</p>

<pre><code>       // _arrOfUsers = array of objects, each object represents a user. 
       //For example, a single object may be something like {name:"mike", id:"123"}    

            var _successUsers = function (_arrOfUsers) {            
                    var promises = [];   //empty array of promises

                        _arrOfUsers.forEach(function (user) {
                            promises.push(function () {
                                return $.Deferred(function (dfd) {
                                     $.post("//sessionID",
                                                queryServ.getAllUserDomainCommentsFunc("//currentDomainID", user.iD, "//currentDomainID", "//currentURL"),
                                        'text', 'text/plain', this,
                                        function (dataX) {
                                            dfd.resolve(dataX);
                                        });
                                }).promise();
                            });
                    });

                    $.when(promises).then(function (results) {
                        console.log(results);
                    });

                };
</code></pre>

<p>Some elements to understand this code:</p>

<p>**** Ajax call settings </p>

<ul>
<li>url: sessionURL</li>
<li>data: data to send to the server is an external call to the module
queryServ.js and the function getAllUserDomainCommentsFunct that
returns a JSON object.  It needs these parameters to work:
currentDomain, userID, currentDomain (again), currentURL</li>
<li>type: text</li>
<li>contentType: text/plain</li>
<li>context: this</li>
</ul>

<p>**** A comment is a jsonObject smilar to <code>{id: "998", textOfcomment:"I commented this", target: "//targetID"}.</code></p>

<p>What I want to do is to make an ajax call for each user of the array "_arrOfUsers" to extract all his comments on the server; after all the "extractions" I need to get all the results (that are all the comments from all users) and print them in the console. The order is important for my code, this is why I'm using Promises.</p>

<p>I found the structure of this algorithm on a JSfiddle here on StackOverflow, but it doesn't work for me because it returns an array of functions and I can't understand why, probably because I've never used Promises during my career.</p>

<p>Thank you in advance, if there's something not clear please feel free to tell me.</p>