Last active
December 20, 2015 01:09
-
-
Save delba/6046557 to your computer and use it in GitHub Desktop.
jQuery promises
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
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