I've been using the bootbox.js library, which operates asynchronously, but I have a need to use it in a synchronous way (for example, putting up a "confirm" window before submitting a form).
I don't want to use e.preventDefault()
to cancel the event, I want to defer the action until the user has responded to the modal. Currently the library doesn't support it, but I am curious if synchronous behavior could be simulated by using promises?
Here's a very basic example, using a link (instead of a form submit button, which is my final goal): http://plnkr.co/edit/5NovsuKTeQ7y6SKNTwWp?p=preview
No, this is not what promises do at all. Promises would be unable to help you here.
The default action for a link is to navigate away to another page (or go to a section in this page). If you want to prevent a link from doing its thing - you have to use e.preventDefault()
or return false from a onclick
handler (preferably the former).
preventDefault
is exactly what you need and sounds exactly like your goal.
confirm
works?!Yes, it does, no one really knows why - blocking was the original behavior of confirm
prompt
and alert
and it was never changed for backwards compatibility. It completely blocks and nothing happens while it's up - this is unlike the language and other APIs and is very uncharacteristic. - For example:
setTimeout(function(){
alert("World");
},100);
alert("Hello");
Here, "World"
will not be aleterd until you close the "Hello" box even though it only has a 100 ms timeout. alert
s and prompt
s simply 'freeze' JavaScript execution. They're considered bad user experience and are generally a poor choice except for very specific problems.
Promises are an abstraction over deferred computation, they allow you to create asynchronous code that looks like it was synchronous which is awesome, but they're not magical and they don't turn the code to be actually synchronous. All promises do is make coding asynchronous code almost as easy as writing synchronous code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With