简体   繁体   English

我可以在Promise中使用console.log()打印某些内容吗? (调试)

[英]Can I print something with console.log() in Promise? (DEBUGGING)

I'm trying to understand someone else's code and here is the original code 我正在尝试理解别人的代码,这是原始代码

// Using Mobx over Redux here. I don't think it's relevant to this problem. 
@action.bound init() { 
    this.time_interval = setInterval(actions.initTime, 1000);

// other file    
export const initTime = () => ({
    server_time: window.time || moment.utc(),
});

And I'm trying to console.log but it gives me an error. 我正在尝试console.log,但这给了我一个错误。

@action.bound init() {
    // I guess it's checking if the current time is same with server's time.
    this.time_interval = setInterval(actions.initTime, 1000);
    console.log(actions.initTime); <- gives me function. 
    console.log(actions.initTime.server_time); <- undefined

// other file    
export const initTime = () => ({ <- This is returning Promise.? I usually used Redux-Thunk to handle all actions and I almost forgot how Promise worked. 
    console.log(window.time); <- syntax error
    server_time: window.time || moment.utc(),
});

So how do we print window.time and moment.utc() ? 那么我们如何打印window.timemoment.utc() Thanks! 谢谢!

UPDATE 更新

export const initTime = () => ({
    server_time: window.time || moment.utc(),
}).then(console.log()); 
^- console.log(window.time) prints undefined
^- console.log(moment.utc()) prints undefined
^- console.log( window_time || moment.utc()) prints a function with few parameters such as 'is_UTC', 'isValid', '_locale'.

Data: 数据:

It doesn't look like its returning a promise there I think this is what you are looking for 看起来好像没有兑现承诺,我认为这就是您想要的

export const initTime = () => {
    console.log(moment.utc())
    return {
      server_time: window.time || moment.utc()
    }
  }
export const initTime = () => ({ <- This is returning Promise.? I usually used Redux-Thunk to handle all actions and I almost forgot how Promise worked. 
    console.log(window.time); <- syntax error
    server_time: window.time || moment.utc(),
});

It happens because you're using implicit return here, and it returns an object. 发生这种情况是因为您在这里使用隐式return,并且它返回一个对象。 You can't use console.log inside an object. 您不能在对象内部使用console.log。

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

相关问题 我可以在console.log中将对象打印为类似于日期的字符串吗? - Can I print Object as string like Date in console.log? 无法在 console.log 中打印 € 符号? - Can't print € symbol in console.log? 在对象上调试console.log - Debugging console.log on object 每当console.log执行时,我想做些事情。 我可以添加事件监听器吗? - I want to do something every time console.log executes. Can I add an event listener? 未捕获(承诺)TypeError:无法设置 null 的属性“innerHTML” - 我可以在 console.log() 中看到值 - Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of null - I can see value in console.log() 如何使log4js打印对象,如console.log - How can I make log4js print objects like console.log does 如何显示 console.log? - How can I display console.log? 我无法将反向数组打印到调用它的 console.log 中 - I can't get my reversed array to print into the console.log calling it 如何使用javascript node.js中的console.log打印函数内的变量列表 - how can I print a list of variables within a function using console.log in javascript node.js 如何使用相同的 console.log 在不同的行上打印多个 arrays? - How can I print multiple arrays on separate lines using same console.log?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM