Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created January 21, 2014 00:47
Show Gist options
  • Save stevenharman/8532288 to your computer and use it in GitHub Desktop.
Save stevenharman/8532288 to your computer and use it in GitHub Desktop.
Wrapping AngularJS $http calls in a promise, while also being able to massage the returned data, is pretty common. Especially when tucking that $http concerns behind a service. So, I made a `defer` helper that will create a promise for me!
angular.module('brewdegaCellar').factory 'ImportMatchReport', ($http, $q) ->
defer = (f)->
deferred = $q.defer()
f(deferred)
return deferred.promise
show: ()->
defer (deferred)->
$http.get('/import/match_report')
.success((data)-> deferred.resolve(data.matchReport))
.error(-> deferred.reject('An error occurred while fetching the match report.'))
confirm: (match)->
defer (deferred)->
$http.post(match.links.confirmation)
.success((data)-> deferred.resolve(data.match))
.error(-> deferred.reject("An error occurred while confirming the Match #{match.id}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment