简体   繁体   English

Javascript 承诺 - 使用/不使用函数捕获

[英]Javascript promises - catch with/without function

I just started learning JavaScript and promises some hours ago, and I'm starting to get the "flow", but a couple of details are still unclear.几个小时前我刚刚开始学习 JavaScript 和 promises,我开始了解“流程”,但一些细节仍然不清楚。 Let's look at the following example:让我们看看下面的例子:

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) 

Result of the above code: This is an error 2上面代码的结果:这是错误2
The example above works just fine.上面的例子工作得很好。 My question is: If I don't call a function and I try to directly call "console.log("this is an error")" inside the catch, why does it fail?我的问题是:如果我不调用函数,而是尝试在 catch 内直接调用“console.log("this is an error")”,为什么会失败? Like this:像这样:

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"))

With the result being:结果是:

(node:39515) UnhandledPromiseRejectionWarning: 3 (节点:39515)未处理的承诺拒绝警告:3
This is an error这是一个错误
(node:39515) UnhandledPromiseRejectionWarning: Unhandled promise rejection. (节点:39515)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 2) (拒绝编号:2)
(node:39515) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (节点:39515)[DEP0018] 弃用警告:不推荐使用未处理的承诺拒绝。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程。
Process finished with exit code 0进程以退出代码 0 结束

More than "promises" I believe my lack of knowledge is about functions in JS, console.log and console.log("whatever").不仅仅是“承诺”,我相信我缺乏关于 JS、console.log 和 console.log(“whatever”) 中的函数的知识。 Any help or advice is really appreciated.任何帮助或建议都非常感谢。

catch() and then() expect to receive a function as argument. catch()then()期望接收一个函数作为参数。 In your example, OnRejected is a function, while console.log("This is an error") is not.在您的示例中, OnRejected是一个函数,而console.log("This is an error")不是。

To explain a bit more : console.log is a function but console.log('something') is the result of the execution of the function console.log with the argument 'something' .再解释一下: console.log是一个函数,但console.log('something')是函数console.log和参数'something'的执行结果。

To go back to catch() and then() , they will call the method you give it (in your example: OnRejected ) and call it with, as an argument, whatever was return by the previously resolved promise.要返回catch()then() ,他们将调用您提供的方法(在您的示例中: OnRejected )并使用先前解决的承诺返回的任何内容作为参数调用它。

Example :例子 :

getDataFromDistantServer().then(function (data) => {
    console.log(data)
    return otherCallToOtherServer()
}).then( function (dataFromOtherServer) => {
    console.log(dataFromOtherServer)
})

This would also work because doSomething is a function:这也可以,因为doSomething是一个函数:

var doSomething = function(data) {
    console.log(data)
    return otherCallToOtherServer()
}

getDataFromDistantServer()
    .then(doSomething)
    .then( function (dataFromOtherServer) => {
        console.log(dataFromOtherServer)
    })

Side note : naming convention for your function OnRejected would dictate to not start the name with a capital letter and call it onRejected旁注:您的函数OnRejected命名约定将规定名称不要以大写字母开头,而是将其onRejected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM