简体   繁体   中英

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. 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
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? 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
This is an error
(node:39515) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 2)
(node:39515) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Process finished with exit code 0

More than "promises" I believe my lack of knowledge is about functions in JS, console.log and console.log("whatever"). Any help or advice is really appreciated.

catch() and then() expect to receive a function as argument. In your example, OnRejected is a function, while console.log("This is an error") is not.

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' .

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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