javascript - 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();
解决方案
有可能,你只是有一个额外的()
之后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
.
推荐阅读
- r - ggmap 包:按变量值缩放时使点大小变大?
- database - 我正在做一个任务,我必须创建只读的 sqlite 数据库,其中不提示用户输入数据
- solr - SOLR,docValues=true,存储=true
- groovy - Gate:groovy中每个循环后的断线
- python - Python - 将数字更改为单词的最简单方法
- ruby-on-rails - 具有简单形式的属性 erb 中的插值(Rails)
- python - 在堆叠矩阵上运行 kendall tau 回归的更快方法?
- html - Pandoc lua 过滤器:如何保留脚注的格式?
- sql-server - 如何从存储过程中返回一个空字段
- html - 图像在 Bootstrap 上没有正确对齐