异步函数中的event.preventDefault [英] event.preventDefault in async functions
问题描述
是否可以在异步函数中使用event.preventDefault?
Will it be possible to use event.preventDefault in an async function?
我不确定,因为必须同步调用event.preventDefault,并且异步函数会返回promise.
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
}
推荐答案
是的,完全有可能在async
事件处理程序function
中调用preventDefault()
.您只需要确保在第一个await
之前进行调用,否则在函数恢复时该事件已经发生.事件流将继续,而不是等待事件处理程序返回的承诺.
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.
这篇关于异步函数中的event.preventDefault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!