Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...