0

I am wondering what is difference between those two services in AngularJS. In both there is promise and basicly both works.

Method with self variable:

module.exports = function () {
var self = this,
    data = false;

self.someFunction = function () {
    methodFromAnotherService().then(function (reponse) {
        data = response;
    });
};
};

Method with binding:

module.exports = function () {
var data = false;

this.someFunction = function () {
    methodFromAnotherService().then(function (reponse) {
        data = response;
    }.bind(this));
};
};

The secound one doesn't work without binding. I know that this have something to do with scope. Please provide any usefull information about major differences.

MacEncrypted
  • 184
  • 9

1 Answers1

1

The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

Gary
  • 2,293
  • 2
  • 25
  • 47