我可以在Promise中使用console.log()打印一些东西吗? (调试)

时间:2018-05-23 06:22:28

标签: javascript reactjs momentjs mobx

我试图了解别人的代码,这里是原始代码

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

数据:

2 个答案:

答案 0 :(得分:1)

它看起来不像是在那里回应了一个承诺 我认为这就是你要找的东西

export const initTime = () => {
    console.log(moment.utc())
    return {
      server_time: window.time || moment.utc()
    }
  }

答案 1 :(得分:1)

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

这是因为你在这里使用隐式返回,它返回一个对象。您不能在对象中使用console.log。