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.

Reserve promise rejections (i.e. async exceptions) for exceptional circumstances

See original GitHub issue

With the browser’s native prompt function, pressing cancel doesn’t result in an error, but instead a null return value. (Similarly, with the native alert function, hitting escape doesn’t result in an error, and with the native confirm function, clicking no doesn’t result in an error but instead a false return value.)

I believe this to be the desired behavior. Especially when using the up-coming async/await feature of ES (being used already via Babel), the current swal2 behavior leads to some awkward control-flow…

let result
let error
try {
  result = await swal({
    text: 'What is your favorite color?'
    input: text,
    showCancelButton: true
  })
} catch (_error) {
  error = _error
}
if (error) {
  swal('I did not get your favorite color because "${error}".')
} else {
  swal(`Your favorite color is ${result}.`)
}

The following would be a bit nicer.

const result = await swal({
  text: 'What is your favorite color?'
  input: text,
  showCancelButton: true
})
if (result.dismiss) {
  swal('I did not get your favorite color because "${result.dismiss}".')
} else {
  swal(`Your favorite color is ${result.value}.`)
}

@limonte Would you review a PR adding an option to enable this behavior? Let’s call the option rejections and have it default to true?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
zenflowcommented, Jan 11, 2018
const {dismiss, value: favoriteColor} = await swal({text: 'What is your favorite color?', input: 'text'})
if (dismiss) {
  swal(`I did not get your favorite color because "${dismiss}".`)
} else {
  swal(`Your favorite color is "${favoriteColor}".`)
}
const {value: name} = await swal({text: 'Full name?', input: 'text'})
const {value: location} = await swal({text: 'Location?', input 'text'})
await swal(`Hi ${name}, from ${location}!`)
2reactions
toveruxcommented, Nov 28, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise-based functions should not throw exceptions - 2ality
This blog post gives tips for error handling in asynchronous, Promise-based functions.
Read more >
Handling those unhandled promise rejections with JS async ...
One of your await ed functions fails (i.e. rejects a promise); You get the error. Another possibility is that you know you need...
Read more >
25. Promises for asynchronous programming - Exploring JS
Rejecting a Promise means that the Promise becomes rejected. ... Furthermore, both exceptions and asynchronous errors are managed the same way.
Read more >
Best Practices for ES6 Promises - DEV Community ‍ ‍
In this article, I will discuss the best practices I have learned over the years that helped me make the most out of...
Read more >
Should I throw an error or return a rejected promise inside an ...
A throw within a Promise/Promise chain will automatically cause that Promise/Promise Chain to be rejected. const myFunc = input => { return ...
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