简体   繁体   中英

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?

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.

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