Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

javascript - Can I "rename" the methods 'then' and 'catch' when I'm returning a promise?

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;
                }

            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...