question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux - How to use react-hot-toast with async await Axios call

See original GitHub issue

hey guys, I saw the other question about this but it did not help me. I have tried multiple ways to get this to work but cannot seem to make it work.

I tried this:

export const postVersionBulkAction = (args) => {
	let url = `${apiBase}/smart-list/version/${args.versionId}/action`
	let promise = queue.wrap(async (dispatch) => {
		let params = { iabCategoriesActions: args.iabCategoriesActions }
		const result = await axios.patch(url, params)
		if (result.status === 200) {
		}
	})
	toast.promise(promise, {
		loading: 'Saving...',
		success: 'saved',
		error: 'error'
	})
}

but that gives me an error: ‘TypeError: promise.then is not a function’

I’ve also tried this:

export const postVersionBulkAction = (args) => {
	let url = `${apiBase}/smart-list/version/${args.versionId}/action`
	return queue.wrap(async (dispatch) => {
		try {
			let params = { iabCategoriesActions: args.iabCategoriesActions }
			const result = await axios.patch(url, params)
			toast.promise(result, {
				loading: 'Saving...',
				success: 'saved',
				error: 'error'
			})
		} catch (error) {
			alert(error)
		}
	})
}

but i get ‘promise.then is not a function’ error on that one too.

I’ve also tried calling it like this:

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

but that way always fires the success toast even if it fails.

Any idea how i could make this work?

PS. thanks for the awesome product! Other than this problem it has helped me so much, and I have succesfully removed thousands of lines of redux boilerplate because of this library. thanks again

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

36reactions
ingusjancommented, Jul 30, 2021

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)

4reactions
gugocharadecommented, Mar 8, 2022

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!',
    });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use react-hot-toast with Promise & Axios - DEV Community ‍ ‍
Use react -hot-toast with Promise & Axios ... default function App() { const fetchData = async () => { const response = await...
Read more >
react toastify with redux from axios API - Stack Overflow
i fixed it and here is my code : // auth.js ( redux page ) export const login = createAsyncThunk( "/login", async ({...
Read more >
Create smoking hot toast notifications in React with ... - Daily.dev
Smoking hot Notifications for React provides lightweight, customizable and beautiful by default notifications. Learn how to use it quickly ...
Read more >
toast() API
toast () API. Call it to create a toast from anywhere, even outside React. Make sure you add the <Toaster/> component to your...
Read more >
How to Use Async/Await with Axios in React | by Aditya Singh
Now, we want to make the same request using async / await . Let's do it! For a function to use await ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found