Created
April 22, 2013 23:32
-
-
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 …
This file contains 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
# 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() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.