4

I have a factory method which has $http(async task) so i used $q promise and the following error has occurred TypeError: object is not a function @line: return $q(funtion....)

written using Promise API:

service.fetch = function(query) {
    return $q(function(resolve, reject){
        $http({ url: srcset[query], method: 'GET'}).success(function(db){
            resolve(db);
        });
    });
};

but if written with Deferred API:

service.fetch = function(query) {
    var deferred = $q.defer();
    $http({ url: srcset[query], method: 'GET'}).success(function(db){
        deferred.resolve(db);
    });
    return deferred.promise;
};

It works fine

I have written exactly as mentioned in https://docs.angularjs.org/api/ng/service/$q

Can anyone point out where have i gone wrong.

3
  • 1
    angular version discrepancy? code.angularjs.org/1.2.25/docs/api/ng/service/$q is different
    – O'Mutt
    Nov 13, 2014 at 16:21
  • ok got it the documentation i referred to is 1.3 build but i was using 1.2.25. Thanks a lot, please post it as an answer many might have been making this mistake Nov 13, 2014 at 16:30
  • added an answer for you so we can mark this as resolved
    – O'Mutt
    Nov 13, 2014 at 16:32

1 Answer 1

6

The docs you link to are the latest and may not be the correct version of angular. Made that mistake before.

1.2.25 (Stable) Docs for $q

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.