1

I have some each iteration by data response from some jquery post action. My question is how to check that this itaration ends?

$.each(data, function(key, value){
//
}).promise().done(function(){
    setTimeout(function(){
        submitBtn.removeClass('buttonInProgress');
    }, time);
});

This is my code, what is wrong? I have an error undefined is not a function.

Lukas
  • 7,384
  • 20
  • 72
  • 127
  • 5
    You should read [the documentation for `$.each()`](http://api.jquery.com/jQuery.each/) - it doesn't work like you think it does. – Pointy Jul 09 '14 at 16:15
  • http://stackoverflow.com/questions/2358205/invoking-a-jquery-function-after-each-has-completed – gulty Jul 09 '14 at 16:16
  • 2
    `.each()` is synchronous, so it is guaranteed to have ended on the next line. You probably have asynchronous methods inside the loop and you want to check when *they* have completed -- you'll have to show that code to get more specific help. – JJJ Jul 09 '14 at 16:19
  • `$.each()` returns the `data` array, not a jQuery object, so `.promise()` isn't a valid method. Without more details about your callback it's hard to help you more than that. – Blazemonger Jul 09 '14 at 16:22
  • im posting only a strings of numbers, the server is cuting them to short value and send back, after this i'm typing it on the rail to the input value – Lukas Jul 09 '14 at 16:22
  • 2
    You will need to show us the asynchronous code inside the `$.each()` for us to be able to help you with your specific issue. Without that, this question probably needs to be closed for not disclosing enough information to provide an actual answer that is specific to your problem. – jfriend00 Jul 09 '14 at 16:30
  • I'm just wondering where people get this notion that a promise gets resolved when any code near it in your source finishes. People seem to think that promises have some magic power like this. A promise needs to be wired up to some action and only when that action specifically resolves your promise will it execute its `.done()` handler. Your are trying to use `.promise()` on something that doesn't even have that method and then you're expecting it to be wired up to the code inside the `$.each()` even though there is no connection between the two. You must show the code inside the `$.each()` – jfriend00 Jul 09 '14 at 19:58

1 Answers1

1

If you're doing some asynchronous action inside the .each callback, it's up to you to build a set of promises, and use $.when to wait for them all to resolve. It's also up to you to manually resolve each promise created inside your .each callback.

The process will look something like this:

var promises = [];

$.each(data, function () {
  var dfd = $.Deferred();

  doSomethingAsync({success: function () { dfd.resolve() });

  promises.push(dfd);
});

$.when.apply($, promises).then(function () {
  setTimeout(function(){
    submitBtn.removeClass('buttonInProgress');
  }, time);
});
user229044
  • 232,980
  • 40
  • 330
  • 338
  • http://api.jquery.com/each/ Regardless, I don't *care* whether it returns a jQuery object. My point is it doesn't return a promise-like object that will have a `done` method, as assumed in the original question. – user229044 Jul 09 '14 at 16:27
  • 2
    Which is very different from http://api.jquery.com/jquery.each -- the error he's getting is directly caused because `.promise()` is a jQuery method, not an array method. – Blazemonger Jul 09 '14 at 16:27
  • you might want to use `map` btw. – Bergi Jul 09 '14 at 16:31
  • 1
    And, async things done in jQuery (like Ajax calls) often already have their own promises which should be used instead of creating new deferreds, though the real answer to this question only comes when/if the OP shows us their async code inside the loop. – jfriend00 Jul 09 '14 at 16:31
  • 2
    Without declaring `dfd` as a variable inside the function you're essentially creating a global property on the `window` object. Likely you'll find weird issues with this, such as only the last object in `data` ever being resolved. Adding `"use strict";` to the top of your file often helps track down bugs like this (although you probably would only want to enable strict mode for that file, so you could wrap the entire thing in an anonymous function that gets called immediately). – redbmk Jul 09 '14 at 16:33