-
-
Save robwormald/eca5cbf5e06b6d46c81b to your computer and use it in GitHub Desktop.
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
//opt 1 | |
SomeService.getStuff().then(function(stuff){ | |
return SomeService.manipulateStuff(stuff); | |
}).then(function(manipulatedStuff){ | |
//do stuff... | |
}); | |
//opt 2 | |
SomeService.getStuff().then(SomeService.manipulateStuff).then(function(manipulatedStuff){ | |
//do stuff... | |
}) | |
//fluent option | |
return SomeService.getStuff() //returns a promise for stuff, will resolve after manipulate and OtherStuff resolve. | |
.then(SomeService.manipulateStuff) //manipulates stuff, and returns it | |
.then(SomeService.doOtherStuff) // Does more things to stuff, and returns it | |
.service('SomeService', function(ApiService){ | |
this.getStuff = function() { | |
return ApiService.get('stuff') | |
} | |
this.manipulateStuff = function(stuff) { | |
var i | |
for (i in stuff.things) { | |
stuff.things[i].index = i | |
} | |
var deferred = promise.defer() | |
deferred.resolve(stuff) | |
return deferred.promise | |
} | |
this.doOtherStuff = function(stuff) { | |
var i | |
for (i in stuff.things) { | |
stuff.things[i].doubleIndex = i+i | |
} | |
var deferred = promise.defer() | |
deferred.resolve(stuff) | |
return deferred.promise | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment