-4

I would like to change the name and write "exito" (success in spanish) instead of .then() and "error" instead of catch().

Here is my code.

How can I manage with this?

var url = 'http://devmsadasds'; var object = null;

            var dataServicio = new dataService(url, object, $scope, $routeParams, $controller, $timeout, $http, $q);
                dataServicio.ajaxPeticion(url, object, $scope, $routeParams, $controller, $timeout, $http, $q)
                 .then( function(response){
                     $scope.datosVista = response;
                 })
                 .catch( function(err){
                     alert(err);
                 });

            }


          function dataService(url, object, $scope, $routeParams, $controller, $timeout, $http, $q) {
                return {
                        ajaxPeticion : ajaxPeticion
                    }

                function ajaxPeticion(url, object, $scope, $routeParams, $controller, $timeout, $http, $q) {
                    if(object === null || object === undefined) {
                        var metodo = 'GET';
                }
                else {
                        var metodo = 'POST';
                }

                    var deferred = $q.defer();
                    var promise = deferred.promise;

                    $http({
                            method: metodo,
                            url: url,
                            data: object,
                            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                    })
                    .then(function(response) {
                        if(response.data.jsonMC.resultado === false || response.status != 200 || response.data.jsonMC.respuesta === null) {
                            deferred.reject(response.data.jsonMC.error);
                        }
                        else {
                            deferred.resolve(response.data.jsonMC.respuesta);
                        }
                    }, function errorCallback(response) {
                        deferred.rejected(response.data.jsonMC.error);
                    });

                    return promise;
                }

            }

2 Answers2

0

You could, but to be honest I cannot really see the utility. You are going to create a way that all the developers (or you) will get used to a method which actually doesn't exist, your code will never be understood from outside people who don't know about this "convention". At the same moment, every library of whatever uses "normal promises", will use the then and not your method.

By the way, this should simply do what you want for simple JavaScript, you can do the same for AngularJS with the $q service:

Promise.prototype.exito = Promise.prototype.then;

Promise.prototype.exito = Promise.prototype.then;
new Promise((resolve, reject) => {
  setTimeout(function() {console.log('finished'); resolve('resolved');}, 2500);
}).exito(function(data){ console.log(data); });

Again. I strongly recommend you to not do this.

quirimmo
  • 9,800
  • 3
  • 30
  • 45
  • Yes, I know it. However, my boss wants everything in spanish. It's awful. Anyway, thank you. – Facundo Zambuto Jun 05 '17 at 19:24
  • 1
    You really should let him know that this is part of a standard interface built into the language. This would be like trying to change the word `if` to `si` – sbking Jun 05 '17 at 19:28
  • @FacundoZambuto try to explain him that his doesn't make absolutely sense. What next? Translating all the JavaScript/AngularJS/jQlite methods? If he can't understand this, the best thing you can do instead of masking all the methods, it is to find a new job – quirimmo Jun 05 '17 at 19:31
  • 1
    As soon as I started to work in this project I had been writing methods with English names such as "getOriginalValue()" or "setCustomerName()". When he noticed that, he ask me for trying to write methods en Spanish... If you could have a look to the directives... It's terrible and disgusting. I mean, I love my language, but programming is in English. – Facundo Zambuto Jun 05 '17 at 19:41
0

It is considered bad practice to mask methods with other names. If you are still considering it, please note "error" also has meaning in English, and it may lead to confusion.

Alon
  • 57
  • 1
  • 7