As show in the example on the docs
<https://docs.angularjs.org/api/ng/service/$q> does not work for me since
it fails with the error
function asyncGreet(name) {
// perform some asynchronous operation, resolve or reject the promise when
appropriate.
return $q(function(resolve, reject) {
setTimeout(function() {
if (okToGreet(name)) {
resolve('Hello, ' + name + '!');
} else {
reject('Greeting ' + name + ' is not allowed.');
}
}, 1000);
});}
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
alert('Success: ' + greeting);}, function(reason) {
alert('Failed: ' + reason);});
$q is not a function
However the following works.
// for the purpose of this example let's assume that variables `$q` and
`okToGreet`// are available in the current lexical scope (they could have been
injected or passed in).
function asyncGreet(name) {
var deferred = $q.defer();
setTimeout(function() {
deferred.notify('About to greet ' + name + '.');
if (okToGreet(name)) {
deferred.resolve('Hello, ' + name + '!');
} else {
deferred.reject('Greeting ' + name + ' is not allowed.');
}
}, 1000);
return deferred.promise;}
var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
alert('Success: ' + greeting);}, function(reason) {
alert('Failed: ' + reason);}, function(update) {
alert('Got notification: ' + update);});
NB: *I am using angular 1.5.8*
--
You received this message because you are subscribed to the Google Groups
"Angular" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.