问题描述
我有一个简单的承诺,但我想知道为什么在我的 catch 方法中我只需要传递“console.log”,如果它发生,它会自动记录该错误?
它是否与 catch 自动给我们一个错误对象的事实有关,还是其他什么?
解决方法
在您的情况下,您会将函数 console.log
传递给 catch 方法。 console.log
方法只是在控制台中打印每个参数。在 catch 方法中,传递的函数将在 promise 出现拒绝/错误时执行。
这是一个传递函数的例子:
function a(method) {
method("Hello World");
}
a(console.log);
在这种情况下,console.log
函数可用作 method
函数中的 a
,因为它已作为参数传递。这就是原因,代码在控制台中打印 Hello World
。
总结起来就是一个例子!
function test() {
if (arguments[0] == "works") {
console.log("COOL");
}
}
test("works");