D
D
Drm2015-09-29 13:09:42
Angular
Drm, 2015-09-29 13:09:42

Why does the Cannot read property 'defer' of undefined error appear?

Good day. I needed to make the timeout option for promice into angular for the $q library.
In the open spaces of the bourgeoisie, I found an article where the ability to create a timeout is added through a decorator ( the article itself )
I did everything according to the instructions, I got the following code:

$provide.decorator('$q', function decorateQ($delegate) {
      // $delegate is the native angular $q service
      var _defer = $delegate.defer;
      // keep the original $q.defer function

      $delegate.defer = function newDefer() {
        var d = _defer(); // create the deferred object as before
        d.promise.timeout = function (ms, callback) {
          setTimeout(function () {
            if (pending) {
              cb(callback);
            }
          }, ms);
        };
        return d;
      };
    });

But an error appears:
Uncaught TypeError: Cannot read property 'defer' of undefined

and if I understand correctly, the interpreter cannot find the defer property in the $q library. So? How to win it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question