I have a simple userAPI service in my angular app:
app.service('userAPI', function ($http) {
this.create = function (user) {
return $http
.post("/api/user", { data: user })
.then(function (promise) { return promise.data })
.catch(function (error) { return error.data })
}
this.read = function (user) {
return $http
.get("/api/user/" + user.id)
.then(function (promise) { return promise.data })
.catch(function (error) { return error.data })
}
this.update = function (user) {
return $http
.patch("/api/user/" + user.id, { data: user })
.then(function (promise) { return promise.data })
.catch(function (error) { return error.data })
}
this.delete = function (user) {
return $http
.delete("/api/user/" + user.id)
.then(function (promise) { return promise.data })
.catch(function (error) { return error.data })
}
})
As you can see, i am repeating same .then() and .catch() functions after each of my $http requests. Ho can i avoid this repitition according to DRY principle?