0

Why my array of functions is not triggered?

Edit: Still nothing:

var actions = [];

$.each(data, function(i, v) {

    actions.push(new Promise(function(resolve, reject) {

        if (_this.apiConversatiosGet(v.app_id)) {
            resolve();
        }
    }));
});

$.when(actions).done(function() {
    console.log("done");
});

and:

apiConversatiosGet: function ($app_id)
{
    var _this = this;

    return $.ajax({
        url: "/api/conversations/" + $app_id,
        type: "get",
        success: function (data) {
                  console.log("ajax");
            $.each(data, function (i, v) {
                _this.contactBox.append(v);
           });
        }
    });

}

as a result in console I got:

done (4) ajax

and should be opposite

2
  • $.when should accept an array of deferred objects. When you push to the array, you need to execute each function and return a deferred (if they all execute ajax calls, you could just return the result of the ajax call). Commented Aug 9, 2016 at 14:47
  • Here's an example: jsfiddle.net/2h5hruut/6 Commented Aug 9, 2016 at 14:52

4 Answers 4

1

Seeing your code you don't need promises.

Just make sure the Ajax/Deferred object is stored in your array, as follows.

var actions = [];

$.each(data, function(i, v) {
    actions.push(_this.apiConversatiosGet(v.app_id));
});

$.when.apply($, actions).done(function() {
    console.log("done");
});


apiConversatiosGet: function($app_id) {
    var _this = this;

    return $.ajax({
        url: "/api/conversations/" + $app_id,
        type: "get",
        success: function(data) {
            console.log("ajax");
            $.each(data, function(i, v) {
                _this.contactBox.append(v);
            });
        }
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

alert("All was done"); is done before ajax success inside promise objects
@RobM It's likely you're calling resolve too early then. You have to resolve in the success callback, not before. See edit for example.
See edit. Next time give us the real code directly !
0

It's a little unclear what you're asking, but if you want to evaluate the function as you push it to the array then you should do something like:

    actions.push(function(){
            alert("0");
    }());

Adding the () to the end of the function will cause it to evaluate the function and use what the function returns as the value instead of the function itself.

2 Comments

I want to execute all functions in actions array (which are async ajax calls) and when all are done trigger alert("All was done");
My approach to running code after the last ajax call has finished is to maintain a counter of how many calls have returned and once they have I then have it call the function saying that it has finished. I'm not sure if that's the best approach, but it's one that works as long as you know how many calls will be returning.
0

                var actions = [];
                actions.push = function(i){
                        alert(i);
                        Array.prototype.push.apply(this,[i]);
                }
                //TODO::BEGIN:Ajax Call
                actions.push(0);
                data = [1,2,3,4,5,6];
                $.each(data, function (i, v) {
                    actions.push(v);
                });
                alert(actions.length);
                //TODO::END:Ajax Call
                $.when.apply($,actions).done(function(){
                    alert("All was done"); 
                });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

you should write prototype of push method.

Comments

0
function processItem(i){
   alert(i);
}

var act = [];

data = [1,2,3,4,5,6];
$.each(data, function (i, v) {        
     act.push(processItem(data[i]));
});    

$.when.apply(null, act).done(function(){
     alert("All was done"); 
});

https://jsfiddle.net/6avom0v4/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.