How do I make sure I'm using Kris Kowal's Q instead of jQuery's Deferred 
implementation? Injecting $q using

app.factory('someservice', ['$http', '$q', function ($http, $q) {...

Injects the $q. However, I can only get jQuery's Deferred implementation. 
So I can't do as shown in the documentation 
<https://docs.angularjs.org/api/ng/service/$q>, I get the error 

*TypeError: $q is not a function*

 

// 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) {
  // 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);
});


But I can do

// 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);
});


When I do *console.log($q)*, I see that it has only has fuctions

   - all(promises)
   - defer:()
   - reject:(reason)
   - 
   - when:(value, callback, errback, progressback)
   
*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.

Reply via email to