myFn
is a function that executes an asynchronous task and triggers the callback upon successful completion.
SearchController.prototype.show = function (query) {
this.searchService.myFn(arg1, this.myCallback); //I want to preserve the reference of `this` here
};
SearchController.prototype.myCallback = function (err, result) {
if(err) {
this.onError(err);
return;
}
this.onSuccess(result);
}
SearchController.prototype.onSuccess = function() {...)
SearchController.prototype.onError = function() {...)
It's evident that the context of this
is lost within the callback. How can I retain the original value of this
from the invocation of myFn
?