首页 > 解决方案 > AsyncFunction.apply 不是函数

问题描述

运行以下代码原因apply(...) is not a function,我不确定为什么。

我觉得调用异步函数是不可能的。那么应用调用行为的正确方法是什么?

async function throwSomething() {
  return "Apple";
}

async function caughtSomething() {
  try {
     return await throwSomething.apply(this, arguments || [])();
  } catch(e) {
     console.log(e);
  }
}

caughtSomething();

标签: javascript

解决方案


有可能,你只是有一个额外的()之后throwSomething.apply(this, arguments || [])

async function throwSomething() {
  return "Apple";
}

async function caughtSomething() {
  try {
     return await throwSomething.apply(this, arguments || []);
  } catch(e) {
     console.log(e);
  }
}

caughtSomething().then(console.log);

错误表示"TypeError: throwSomething.apply(...) is not a function",这意味着 的返回值throwSomething.apply(...)不是函数。如果.apply不是一个函数,错误会说"TypeError: throwSomething.apply is not a function.


推荐阅读