1

I have a method from a Provider that looks similar to this:

updateUser(user: User): Promise<User> {
    return this.http.put<any>('url',JSON.stringify(user))
        .toPromise()
        .then((result) => {
            this.eventsService.broadcast('userEdit:success', user)
            return Promise.resolve(true);
        })
        .catch((err) => {
            let errorData = {
                error: err,
                data: user
            };
            this.eventsService.broadcast('userEdit:failed', errorData)
            return Promise.reject(errorData);
        });
}

This does work and I do get the value of user. But I believe that user's value is expected to be gone which would cause an error? How does the variable user retain it's value after the function updateUser?

I wanted to have the user's value back especially on the error catching so that I can notify and take the value to repopulate the form if I navigate back to it with the notification.

The current code works but I believe there is a better/correct way to do this to achieve the same objective. Please let me know how you would rather do it yourself.

Alex Pappas
  • 2,377
  • 3
  • 24
  • 48
  • Why do I have to repopulate the form incase of failure? I will just show notification to user that it is failed. – Just code Nov 15 '18 at 06:43
  • Pass the object as `var user = {persistUser:{}}` from where you call `updateUser` function. and inside the success handler set the value `user.persistUser = user` – front_end_dev Nov 15 '18 at 06:43
  • After the user clicks "Save" I will navigate him back to whatever page so that he can continue working on other stuffs while the request is still not done yet. When that request fails, I want to save the user from the inconvenience of repopulating again the form with his desired changes by just letting him click the notification to redirect him back to the form. – Alex Pappas Nov 15 '18 at 06:58
  • The problem is when there are multiple request not done yet. I will have to collect the `user`s submitted and when one request fails, I have to know which of the saved/persisted user does the error refers to. @front_end_dev – Alex Pappas Nov 15 '18 at 07:02

0 Answers0