Last active
August 29, 2015 13:56
-
-
Save athap/9085647 to your computer and use it in GitHub Desktop.
Wait for N or Multiple or Unknown number (Depends on some logic) of ajax request using JQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Makes ajax req to fetch data | |
class Model | |
initialize(id) | |
@id = id | |
load: (token) -> | |
$.ajax({ | |
url: "foo/bar/#{@id}", | |
.... | |
success: (response) => | |
token.resolve | |
}) | |
class Test | |
waitForNCalls: (ids) -> | |
tokens = [] | |
for id in ids | |
token = $.Deferred | |
loader = new Model(id) | |
loader.load(token) | |
tokens.push(token) | |
return tokens | |
# Here is where the magic happens, it waits for all the tokens to get resolved | |
$.when.apply(this, @waitForNCalls([1,2,3....N])).done( => | |
# Show RED BLINK | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment