Reserve promise rejections (i.e. async exceptions) for exceptional circumstances
See original GitHub issueWith 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:
- Created 6 years ago
- Reactions:5
- Comments:11 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
rude answer to rude message: https://gist.github.com/Rich-Harris/0b6f317657f5167663b493c722647221