AngularJS Promises - Simulate http promises

angularjs, promise

Solution

Ok, I could figure out how to simulate the same promise returned by the $http object. Thanks to all for your answers. I could take them all into consideration. Here is my solution :

if ( !ng.isString(email) ) {

    // We need to create a promise like the one returned by the 
    // $http object (with the success and error methods)
    // to stay consistent.
    var promise = $q.reject("error with email");

    // Defining success and error method for callbacks. 
    // but it should never be called since the promise 
    // is already rejected.
    promise.success = function(fn){
       promise.then(function(response){
          fn(response)
       }, null);
          return promise
    };

    promise.error = function(fn){
       promise.then(null, function(response){
          fn(response)
       });
       return promise;
    };

    return promise;
}

return $http( {
         method : "PUT",
         url : "//localhost/update" ,
         data : { data: email } 
})

// Success handler
.success(response){ return response})

// Error handler
.error(errorMsg){ return errorMsg});

Problem

I wonder how to simulate a promise $http when I know that the request will fail on the server-side. Here is my code: ``` if ( !ng.isString(email) ) { var promise = $q.defer().promise; $q.reject(); return promise; } return $http( { method : "PUT", url : "//localhost/update" , data : { data: email } }) // Success handler .success(response){ return response}) // Error handler .error(errorMsg){ return errorMsg}); ```

Original source

Related problems