Skip to content

Instantly share code, notes, and snippets.

@athap
Last active August 29, 2015 13:56
Show Gist options
  • Save athap/9085647 to your computer and use it in GitHub Desktop.
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
# 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