Skip to content

Instantly share code, notes, and snippets.

@delba
Last active December 20, 2015 01:09
Show Gist options
  • Save delba/6046557 to your computer and use it in GitHub Desktop.
Save delba/6046557 to your computer and use it in GitHub Desktop.
jQuery promises
City =
find: (location) ->
promise = $.Deferred()
$.ajax '/cities',
data: q: location
success: (result) ->
promise.resolve result.city
error: ->
promise.reject 'invalid location'
promise
Weather =
today: (location) ->
promise = $.Deferred()
$.ajax '/weather',
data: q: location
success: (result) ->
promise.resolve result.weather
error: ->
promise.reject 'invalid location'
promise
$('button').on 'click', (e) ->
e.preventDefault()
loc = $(this).data('loc')
$.when(
Weather.today(loc),
City.find(loc)
).then (weatherResult, cityResult) ->
$('#results').append cityResult, weatherResult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment