[英]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.time
和moment.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.