Created
September 30, 2013 07:23
-
-
Save michaelavila/6760369 to your computer and use it in GitHub Desktop.
Orc vs Promises 1
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
getData = -> | |
xhr new XMLHttpRequest | |
xhr.open 'GET', 'data', true | |
handleXMLHTTPRequest = -> | |
if xhr.status is not 200 | |
orc.fail() | |
xhr.addEventListener 'load', orc.waitFor(handleXMLHTTPRequest), false | |
xhr.send() | |
orc.sequence (-> getData(); getLocation()), (data, location) -> | |
alert "We got data: #{data} and location: #{location}" |
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
getData = -> | |
deferred = $.Deferred() | |
xhr = new XMLHttpRequest | |
xhr.open 'GET', 'data', true | |
handleXMLHTTPRequest = -> | |
if xhr.status is 200 | |
deferred.resolve xhr.response | |
else | |
deferred.reject "HTTP error: #{xhr.status}"), | |
xhr.addEventListener 'load', handleXMLHTTPRequest, false | |
xhr.send() | |
deferred.promise() | |
combinedPromise = $.when getData(), getLocation() | |
combinedPromise.done (data, location) -> | |
alert "We got data: #{data} and location: #{location}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The promises example was taken from http://www.html5rocks.com/en/tutorials/async/deferred/