判断是否是异步函数

作者:xie392地址:https://v.douyin.com/iem4sRs7/更新时间:2024-12-21

案例代码

1
function isAsync(fn) {
2
return fn[Symbol.toStringTag] === 'AsyncFunction'
3
}
4
5
console.log(isAsync(async () => {})) // true
6
console.log(isAsync(() => {})) // false

输出结果:

1
true
2
false