Created
December 8, 2015 15:51
-
-
Save ericterpstra/69dfafa14ba44831f654 to your computer and use it in GitHub Desktop.
This file contains 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
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