Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 1.05 KB

is-async-function.md

File metadata and controls

33 lines (21 loc) · 1.05 KB
标题 标签
isAsyncFunction(是否是异步函数) type,function(类型,函数)

检查给定参数是否是异步函数。

  • 使用 Object.prototype.toString() 和 Function.prototype.call() 并检查结果是否为 '[object AsyncFunction]'。
const isAsyncFunction = val =>
  Object.prototype.toString.call(val) === '[object AsyncFunction]';

ts 代码如下:

调用方式:

isAsyncFunction(function () {}); // false
isAsyncFunction(async function () {}); // true

应用场景

以下是一个实战示例:

结果如下:

<iframe src="codes/javascript/html/is-async-function.html"></iframe>