How console.log
is printing the output hence it's a console method and needs to be used with parenthesis. Now I feel there is something interesting about which I'm not aware.
For example:
new Promise((resolve)=> resolve(5))
.then(data=> console.log(data))
.then(Promise.resolve(10).then(console.log));
// Output: 5 10
Can someone explain how it's working and why?
The first argument to Promise#then
is a function to be called when the Promise is fulfilled. It is called with one argument: the value that the Promise resolved to.
Passing console.log
as the first argument means that console.log
will be called and passed the resolved value when the Promise is fulfilled.
It is similar to the following code:
function fn(callback) {
setTimeout(() => callback("value"), 500);
}
fn(console.log);
// equivalent to fn(val => console.log(val))