Skip to content

Instantly share code, notes, and snippets.

@nbryan
Created April 22, 2013 23:32
Show Gist options
  • Save nbryan/5439499 to your computer and use it in GitHub Desktop.
Save nbryan/5439499 to your computer and use it in GitHub Desktop.
This is in CoffeeScript rather than JavaScript, but hopefully it should be clear. In our app we typically have to load multiple things before rendering a page. Using jQuery's Deferred objects the pattern is very simple. This is probably the most basic example we use. I've got some more interesting cases as well, such as fetching many records in …
# With deferred objects
$.when(loadSomething1(), loadSomething2(), loadSomethingElse())
.done(-> renderPage())
.fail(-> renderFailure())
# With callbacks, can't operate in parallel, error cases repeated
loadSomething1(
success: loadSomething2(
success: loadSomethingElse(
success: renderPage(),
error: renderFailure()
),
error: renderFailure()
),
error: renderFailure()
)
@cfurrow
Copy link

cfurrow commented Apr 23, 2013

For this case, the choice is certainly clear. The DOM Futures examples I saw did not involve multiple async calls, and then success/fail callbacks that fire after the n-number of callbacks fired/completed.

Thanks for the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment