Code Monkey home page Code Monkey logo

Comments (11)

ingusjan avatar ingusjan commented on June 9, 2024 37

Have you tried ensuring your .catch was first and .then following it? Weirdly enough, that fixed this same issue for me.

 const callMyFunction = callFunction({ username: this.state.username })
      .catch((error) => {
        // Getting the Error details.
        console.error(error.message);
        return error.message;
      })
      .then((res) => {
        console.log(res);
        refreshData();
      });

    toast.promise(callMyFunction, {
      loading: "Loading...",
      success: "Everything went smoothly.",
      error: "Uh oh, there was an error!",
    });

(I know this isn't an Axios example, but the promise structure is relatively the same)

from react-hot-toast.

gugocharade avatar gugocharade commented on June 9, 2024 10

This worked for me

const res = fetch(apiUrl);

    toast.promise(res, {
      loading: 'Loading ...',
      success: (data) => {
        console.log(data);
        if (data.status === 500) throw new Error('server error');
        return 'Everything went smoothly.';
      },
      error: 'Uh oh, there was an error!',
    });

from react-hot-toast.

gamino97 avatar gamino97 commented on June 9, 2024 3

The toast.promise function returns the promise, so you could use something like this:

const callMyFunction = toast.promise(callFunction({ username: this.state.username }), {
      loading: "Loading...",
      success: "Everything went smoothly.",
      error: "Uh oh, there was an error!",
    });
callMyFunction.then((res) => {
    console.log(res);
    refreshData();
})
 .catch((error) => {
        // Getting the Error details.
        console.error(error.message);
        return error.message;
      });
      

    

from react-hot-toast.

TheJazzDev avatar TheJazzDev commented on June 9, 2024 2

In case anyone using firebase:

 toast.promise(
      createUserWithEmailAndPassword(authInstance, email, password),
      {
        loading: "loading...",
        success: (result) => `success, ${result.user}`,
        error: (e) => `error ${e.message}`,
      }
    );

Thanks a lot, this work for me...

from react-hot-toast.

timolins avatar timolins commented on June 9, 2024

Hey! I'm not entirely sure what is going on but I think it should work like this:

const promise = axios.patch(url, params)

toast.promise(promise, {
  loading: 'Saving...',
  success: <b>saved!</b>,
  error: <b>Could not apply.</b>
})

const result = await promise

from react-hot-toast.

Jared-Dahlke avatar Jared-Dahlke commented on June 9, 2024

Hey! I'm not entirely sure what is going on but I think it should work like this:

const promise = axios.patch(url, params)

toast.promise(promise, {
  loading: 'Saving...',
  success: <b>saved!</b>,
  error: <b>Could not apply.</b>
})

const result = await promise

It is still popping up as a success when I try your way even when the network call is failing with a 404:

export const postVersionBulkAction = (args) => {	
	return async (dispatch) => {		
		const promise = axios.patch(badUrl,  params )
		toast.promise(promise, {
			loading: 'Saving...',
			success: 'saved',
			error: 'error'
		})

		const result = await promise
	}
}

it pops up a success immediately

Edited: removed some of the extraneous stuff from the function to make it easier for someone to diagnose

from react-hot-toast.

lsbyerley avatar lsbyerley commented on June 9, 2024

@JaredDahlke did you ever resolve this? I am also seeing this error when trying to write a unit test for a component using toast.promise

from react-hot-toast.

tlhabane avatar tlhabane commented on June 9, 2024

Here is what works for

const promise = axios.patch(url, params)

toast.promise(promise, {
  loading: 'Saving...',
  success: <b>saved!</b>,
  error: <b>Could not apply.</b>
},
, {
   style: {
    minWidth: '250px',
   }
 }
)
.then((r) => r)
.catch((error) => error);

from react-hot-toast.

amanzrx4 avatar amanzrx4 commented on June 9, 2024

In case anyone using firebase:

 toast.promise(
      createUserWithEmailAndPassword(authInstance, email, password),
      {
        loading: "loading...",
        success: (result) => `success, ${result.user}`,
        error: (e) => `error ${e.message}`,
      }
    );

from react-hot-toast.

axquired24 avatar axquired24 commented on June 9, 2024

In my case, make sure you throw an error after doing a catch

This Work for me

 const callMyFunction = callFunction({ username: this.state.username })
      .catch((error) => {
        // Getting the Error details.
        console.error(error.message);
        throw error; // throw again after you catch
      })
      .then((res) => {
        console.log(res);
        refreshData();
      });

    toast.promise(callMyFunction, {
      loading: "Loading...",
      success: "Everything went smoothly.",
      error: "Uh oh, there was an error!",
    });

from react-hot-toast.

a0m0rajab avatar a0m0rajab commented on June 9, 2024

This is a bit tricky due to the next:

  • The server errors are not handled as an error within javascript
  • We expect errors from the server as normal error

I solved this by doing the next: throwing an error from the my toast code if the server was not sending the expected/ok statues.

toast.promise(fetch(someURL), {
      loading: "Loading ...",
      success: data => {
        if (!data.ok) { //here you can that this will throw the error from the returned data. Usually it's treated as normal thing.
          throw new Error(`Statues code ${data.status}`);
        }
        return "Everything went good";
      },
      error: e => {
        return `Uh oh, there was an error! ${e.message}`;
      },
    })

from react-hot-toast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.