Javascript promises - 捕获 with/without 函数
Javascript promises - catch with/without function
几个小时前我才开始学习 JavaScript 和 promises,现在我开始学习 "flow",但一些细节仍然不清楚。让我们看下面的例子:
function OnRejected(var){
return console.log("This is an error " + var)
}
Promise.reject(2).then(function(a){return a*2})
.then(function(a){return a+5})
.then(function(a){return a*3})
.then(console.log)
.catch(OnRejected)
以上代码的结果:这是一个错误2
上面的例子工作得很好。我的问题是:如果我不调用函数,而是尝试直接在 catch 中调用 "console.log("this is an error")",为什么会失败?像这样:
Promise.reject(3).then(function(a){return a*2})
.then(function(a){return a+5})
.then(function(a){return a*3})
.then(console.log)
.catch(console.log("This is an error"))
结果为:
(node:39515) UnhandledPromiseRejectionWarning: 3
这是一个错误
(node:39515) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是在没有 catch 块的情况下在异步函数内部抛出,要么是因为拒绝了一个没有用 .catch() 处理的承诺。 (拒绝编号:2)
(节点:39515)[DEP0018] DeprecationWarning:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。
进程已结束,退出代码为 0
超过"promises" 我认为我缺乏关于 JS 中函数的知识,console.log 和 console.log("whatever")。非常感谢任何帮助或建议。
catch()
和 then()
期望接收一个函数作为参数。在您的示例中,OnRejected
是一个函数,而 console.log("This is an error")
不是。
再解释一下:console.log
是一个函数,但 console.log('something')
是函数 console.log
和参数 'something'
执行的结果。
要回到 catch()
和 then()
,他们将调用您提供的方法(在您的示例中:OnRejected
)并调用它,作为参数,无论是 return 之前解决的承诺。
示例:
getDataFromDistantServer().then(function (data) => {
console.log(data)
return otherCallToOtherServer()
}).then( function (dataFromOtherServer) => {
console.log(dataFromOtherServer)
})
这也可行,因为 doSomething
是一个函数:
var doSomething = function(data) {
console.log(data)
return otherCallToOtherServer()
}
getDataFromDistantServer()
.then(doSomething)
.then( function (dataFromOtherServer) => {
console.log(dataFromOtherServer)
})
旁注:您的函数的命名约定OnRejected
将规定名称不要以大写字母开头并将其命名为onRejected
几个小时前我才开始学习 JavaScript 和 promises,现在我开始学习 "flow",但一些细节仍然不清楚。让我们看下面的例子:
function OnRejected(var){
return console.log("This is an error " + var)
}
Promise.reject(2).then(function(a){return a*2})
.then(function(a){return a+5})
.then(function(a){return a*3})
.then(console.log)
.catch(OnRejected)
以上代码的结果:这是一个错误2
上面的例子工作得很好。我的问题是:如果我不调用函数,而是尝试直接在 catch 中调用 "console.log("this is an error")",为什么会失败?像这样:
Promise.reject(3).then(function(a){return a*2})
.then(function(a){return a+5})
.then(function(a){return a*3})
.then(console.log)
.catch(console.log("This is an error"))
结果为:
(node:39515) UnhandledPromiseRejectionWarning: 3
这是一个错误
(node:39515) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是在没有 catch 块的情况下在异步函数内部抛出,要么是因为拒绝了一个没有用 .catch() 处理的承诺。 (拒绝编号:2)
(节点:39515)[DEP0018] DeprecationWarning:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。
进程已结束,退出代码为 0
超过"promises" 我认为我缺乏关于 JS 中函数的知识,console.log 和 console.log("whatever")。非常感谢任何帮助或建议。
catch()
和 then()
期望接收一个函数作为参数。在您的示例中,OnRejected
是一个函数,而 console.log("This is an error")
不是。
再解释一下:console.log
是一个函数,但 console.log('something')
是函数 console.log
和参数 'something'
执行的结果。
要回到 catch()
和 then()
,他们将调用您提供的方法(在您的示例中:OnRejected
)并调用它,作为参数,无论是 return 之前解决的承诺。
示例:
getDataFromDistantServer().then(function (data) => {
console.log(data)
return otherCallToOtherServer()
}).then( function (dataFromOtherServer) => {
console.log(dataFromOtherServer)
})
这也可行,因为 doSomething
是一个函数:
var doSomething = function(data) {
console.log(data)
return otherCallToOtherServer()
}
getDataFromDistantServer()
.then(doSomething)
.then( function (dataFromOtherServer) => {
console.log(dataFromOtherServer)
})
旁注:您的函数的命名约定OnRejected
将规定名称不要以大写字母开头并将其命名为onRejected