繁体   English   中英

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

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

我正在尝试理解别人的代码,这是原始代码

// 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(),
});

我正在尝试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(),
});

那么我们如何打印window.timemoment.utc() 谢谢!

更新

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'.

数据:

看起来好像没有兑现承诺,我认为这就是您想要的

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(),
});

发生这种情况是因为您在这里使用隐式return,并且它返回一个对象。 您不能在对象内部使用console.log。

暂无
暂无

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

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