Event.preventDefault in asynchronous functions

Is it possible to use the event.preventDefault function in an asynchronous function?

I'm not sure, because event.preventDefault should be called synchronously, and async functions return promises.

self.oncontextmenu = async function(event) {
 event.preventDefault()
 //await whatever
}
+2
source share
1 answer

Yes, it is quite possible to trigger preventDefault()events in the handler async function. You only need to make a call before the first await, because otherwise the event will already occur when the function resumes. The flow of events will continue and do not wait for the promise that the event handler returns.

+5
source

Source: https://habr.com/ru/post/1691637/


All Articles