Skip to content

Instantly share code, notes, and snippets.

@michaelavila
Created September 30, 2013 07:23
Show Gist options
  • Save michaelavila/6760369 to your computer and use it in GitHub Desktop.
Save michaelavila/6760369 to your computer and use it in GitHub Desktop.
Orc vs Promises 1
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}"
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}"
@michaelavila
Copy link
Author

The promises example was taken from http://www.html5rocks.com/en/tutorials/async/deferred/

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