Mukesh
Mukesh

Reputation: 1267

console.log print value without passing any argument to it

I'm going through promise-it-wont-hurt course on http://nodeschool.io/. Below is the solution of assignment promise_after_promise

'use strict';

/* global first, second */

var firstPromise = first();

var secondPromise = firstPromise.then(function (val) {
  return second(val);
});

secondPromise.then(console.log);

// As an alternative to the code above, ou could also do this:
// first().then(second).then(console.log);

they are not passing any value to console.log but it still print the value how?

Upvotes: 0

Views: 515

Answers (1)

solendil
solendil

Reputation: 8468

promise.then takes a function (two actually, but only one is used here). Then it calls this function with the result of the resolved promise. In this case, console.log is a function, which is called with the result of the resolved promise.

The easier-to-understand alternative would have been

secondPromise.then(function(result) {
  console.log(result);
});

But it creates an unnecessary function.

Upvotes: 3

Related Questions