Skip to content

Instantly share code, notes, and snippets.

@ericterpstra
Created December 8, 2015 15:51
Show Gist options
  • Save ericterpstra/69dfafa14ba44831f654 to your computer and use it in GitHub Desktop.
Save ericterpstra/69dfafa14ba44831f654 to your computer and use it in GitHub Desktop.
function getSomeData() {
// perform some asynchronous operation, resolve or reject the promise when appropriate.
return $q(function(resolve, reject) {
$http({
method: 'GET',
url: '/someUrl'
})
.then(function successCallback(response) {
resolve(respose);
}, function errorCallback(response) {
reject(respose);
});
});
}
function doStuffBeforeGettingData(stuff) {
return $q(function(resolve, reject) {
// Validate stuff.
if (stuffnotvalid) {
// Create error reasons
reject(reasons)
} else {
getSomeData().then(resolve, reject)
}
});
}
// In a Controller or something...
$myService.doStuffBeforeGettingData(things)
.then(function(results){
// do stuff with results
},
function(error){
// show error to user
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment