简体   繁体   中英

event.preventDefault in async functions

Will it be possible to use event.preventDefault in an async function?

I am not sure because event.preventDefault must be called synchronously and async functions return promises.

self.oncontextmenu = async function(event) {
 event.preventDefault()
 //await whatever
}

Yes, it is totally possible to call preventDefault() in an async event handler function . You only have to ensure to make the call before the first await , as otherwise the event already will have happened when the function resumes. The event flow will continue and not wait for the promise that the event handler returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM