<分区>
我觉得有点奇怪,解决这样的 promise
.then(console.log, console.log)
不行,但是这个行
.then(d => {
console.log(d);
}, err => {
console.log(err);
});
我错过了什么
<分区>
我觉得有点奇怪,解决这样的 promise
.then(console.log, console.log)
不行,但是这个行
.then(d => {
console.log(d);
}, err => {
console.log(err);
});
我错过了什么
最佳答案
console.log()
函数需要 console
对象作为 this
的值,否则它不会/不能'不工作。您使用第二位代码实现了这一点,因为您像往常一样调用 console.log()
。但是,在第一段代码中,您将函数的引用从对象本身“剥离”出来,因此当 promise 机制调用函数时,它无法知道它应该安排 this
成为 console
对象。
你也可以这样做
.then(console.log.bind(console), console.log.bind(console))
虽然那很丑:)
关于javascript - 使用 console.log 作为 promise 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761166/
相关文章:
javascript - 除了属性之外,还为解构对象创建变量
javascript - 需要帮助弄清楚如何编写带有条件的 Promise
javascript - AngularJS - 在进度条 CSS 期间绘制边框
javascript - JS 中的 [{}].concat() 是做什么的?
javascript - AngularJS 从对象数组到函数数组
javascript - Bootstrap 4 : How to show one collapsible item and hide others?
javascript - 函数参数在 return new Promise(function(resolve,reject) 内不可见
node.js - NodeJS : Are callback functions or synchronous functions better to use in async/await?