异步函数中的event.preventDefault [英] event.preventDefault in async functions

查看:85
本文介绍了异步函数中的event.preventDefault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在异步函数中使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆