Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with async/await #21

Closed
mrtnpro opened this issue Jan 4, 2021 · 2 comments
Closed

Example with async/await #21

mrtnpro opened this issue Jan 4, 2021 · 2 comments

Comments

@mrtnpro
Copy link

mrtnpro commented Jan 4, 2021

I'm wondering what to give notify.promise when using async/await where I need to access the result. Could you give us an example?

Example:

const onSubmit = async (data) => {
  const { key } = await createProject(data)

  // Now here comes the question:
  notify.promise('What would I possibly use here', {
    loading: 'Loading',
    success: 'Got the data',
    error: 'Error when fetching',
  })

  push(`/projects/${key}`)
}
@mrtnpro
Copy link
Author

mrtnpro commented Jan 4, 2021

Sometimes all you need is to type your question down and your thinking barrier is gone.
It's as simple as that:

const onSubmit = async (data) => {
  const promise = createProject(data)

  const { key } = await promise

  notify.promise(promise, {
    loading: 'Loading',
    success: 'Got the data',
    error: 'Error when fetching',
  })

  push(`/projects/${key}`)

@mrtnpro mrtnpro closed this as completed Jan 4, 2021
@timolins
Copy link
Owner

timolins commented Jan 5, 2021

Yes you're right! That's how you do it. One thing I'd add is that you should call toast.promise first so the loading animation will work.

const onSubmit = async (data) => {
  const promise = createProject(data)

  toast.promise(promise, {
    loading: 'Loading',
    success: 'Got the data',
    error: 'Error when fetching',
  })
  
  const { key } = await promise
    
  push(`/projects/${key}`)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants