Skip to content

Instantly share code, notes, and snippets.

@brian428
Created October 3, 2012 05:11
Show Gist options
  • Save brian428/3825143 to your computer and use it in GitHub Desktop.
Save brian428/3825143 to your computer and use it in GitHub Desktop.
# Using a Promise for a chain of async calls
loadInitialData: ->
@companyService.loadInitialData().then(
success: ( records ) ->
# Do something with results
failure: ( error ) ->
# Do something with the error
).always( ->
# Do something whether the call succeeded or failed
)
# Service method aggregating Promises for separate calls and returning a single Promise
loadInitialData: ->
return Deft.Promise.all( [
@loadCompanies()
@loadFeaturedProducts()
] )
loadCompanies: ->
deferred = Ext.create( 'Deft.Deferred' )
@companyStore.load(
callback: ( records, operation, success ) ->
if( success )
deferred.resolve( records )
else
deferred.reject( "Error loading Companies." )
)
return deferred.promise
loadFeaturedProducts: ->
deferred = Ext.create( 'Deft.Deferred' )
@featuredProductStore.load(
callback: ( records, operation, success ) ->
if( success )
deferred.resolve( records )
else
deferred.reject( "Error loading Products." )
)
return deferred.promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment