node.js - 与 arguments.callee 一起使用的间隔打破了“要求”行为
问题描述
我对为什么有些“要求”感到相当困惑
返回错误:
TypeError: require is not a function at Timeout._onTimeout (.......index.js:8:18)
执行以下操作时:
(()=> {
console.time("pipeline")
pipeline().then((result) => {
console.log("Then: " + result)
console.log("Work Complete for iteration: " + i + " calling iteration no:", i = i + 1)
setTimeout(arguments.callee, 1000);
}).catch((error) => {
console.error("error occured with promise resolution: " + error)
});
console.timeEnd("pipeline")
})()
它运行一次然后出错(即使我显然已连接到数据库)
但更改为这种格式时按预期工作:
(function () {
console.time("pipeline")
pipeline().then((result) => {
console.log("Then: " + result)
console.log("Work Complete for iteration: " + i + " calling iteration no:", i = i + 1)
setTimeout(arguments.callee, 1000);
}).catch((error) => {
console.error("error occured with promise resolution: " + error)
});
console.timeEnd("pipeline")
})()
该错误表明这与我认为的超时有关,因为它在引发错误之前执行一次。
为什么会发生这种行为?这与 arguments.callee 有关吗?如果是,为什么?
提前致谢,
解决方案
arguments
在箭头函数的情况下指父函数范围。箭头函数没有自己的arguments
.
Node.js 模块在底层使用模块包装器函数进行包装:
(function(exports, require, module, __filename, __dirname) {
// Module code actually lives in here
});
这就是arguments.callee
指内部箭头功能。特别是,调用arguments.callee
会导致使用错误的参数再次评估当前模块require
。
依赖arguments.callee
是hacky。更好的方法是显式引用一个函数:
(function foo() {
...
setTimeout(foo, 1000);
...
})()
虽然箭头将要求块范围不泄漏foo
到父范围:
{
let foo;
(foo = () => {
...
setTimeout(foo, 1000);
...
})()
}
推荐阅读
- mysql - SQL - 在另一个表中选择不存在的行
- android - 更新到 firebase-messaging:17.5.0 后出现 AutoAnnotation 错误
- .htaccess - htaccess 变量不显示任何内容
- r - 如何在 kableExtra 中保持标题固定
- sql-server - 如何通过具有波浪号分隔值的列连接表
- tensorboard - TensorFlow 2.0 - 图表未在 TensorBoard 中显示
- spring-boot - 如何在单元测试期间通过 Thymeleasf 模板的身份验证
- python - 将 XML 数据解析为 pandas 多索引数据帧
- r - 在R中创建一个与excel数据透视表相同的数据透视表
- python - for i in range(5) and k in range(2): TypeError: 'bool' object is not iterable