简体   繁体   English

console.log打印值,不传递任何参数

[英]console.log print value without passing any argument to it

I'm going through promise-it-wont-hurt course on http://nodeschool.io/ . 我正在通过http://nodeschool.io/上的promise-it-wont-hurt课程。 Below is the solution of assignment promise_after_promise 以下是分配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? 他们没有将任何值传递给console.log,但是它仍然显示值如何?

promise.then takes a function (two actually, but only one is used here). promise.then需要一个函数(实际上有两个,但这里只使用了一个)。 Then it calls this function with the result of the resolved promise. 然后,它以已解决的promise的结果调用此函数。 In this case, console.log is a function, which is called with the result of the resolved promise. 在这种情况下, console.log是一个函数,该函数以已解决的promise的结果进行调用。

The easier-to-understand alternative would have been 更容易理解的替代方案是

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

But it creates an unnecessary function. 但是它创建了不必要的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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