-1

I have a simple promise, but I wonder why in my catch method I only need to pass "console.log" and it will automatically log that error if it happens? enter image description here

Does it have something to do with the fact that catch automatically gives us an error object or is it something else?

arthasnp98
  • 13
  • 1
  • 6
  • Actually passing `console.log` as callback, still works because is also a function, instead of creating extra function to handle to logs – Naren Jan 20 '21 at 11:45
  • Why should this not work? Asking this question, there's obviously *something* that you didn't fully understand about JS, but I can not tell what exactly this *something* is. – Thomas Jan 20 '21 at 11:54

2 Answers2

2

In your case, you would pass the function console.log to the catch method. console.log method just prints each parameter in the console. Inside the catch method, the passed function will be executed on a reject/error of the promise.

Here is an example of passing functions:


function a(method) {
    method("Hello World");
}

a(console.log);

In this case, the console.log function is available as method in the a function as it has been passed as a parameter. This is the reason, the code prints Hello World in the console.

EricHier
  • 446
  • 3
  • 10
-2

Hidden arguments to sum it up here's an example!

function test() {
    if (arguments[0] == "works") {
        console.log("COOL");
    }
}

test("works");
BGPHiJACK
  • 1,277
  • 1
  • 8
  • 16