Skip to content

Instantly share code, notes, and snippets.

@wmfairuz
Created August 19, 2014 01:59
Show Gist options
  • Save wmfairuz/80151c8285b8ca7d469a to your computer and use it in GitHub Desktop.
Save wmfairuz/80151c8285b8ca7d469a to your computer and use it in GitHub Desktop.
promise
function deferredTimer(success) {
var deferred = $q.defer();
$timeout(function() {
if (success) {
deferred.resolve({ message: "This is great!" });
} else {
deferred.reject({ message: "Really bad" });
}
}, 1000);
return deferred.promise;
}
$scope.startDeferredTimer = function(success) {
deferredTimer(success).then(
function(data) {
$scope.deferredTimerResult = "Successfully finished: " +
data.message;
},
function(data) {
$scope.deferredTimerResult = "Failed: " + data.message;
}
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment