我可以在 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.time
和 moment.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,并且它 return 是一个对象。您不能在对象内使用 console.log。
我正在尝试理解别人的代码,这是原始代码
// 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.time
和 moment.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,并且它 return 是一个对象。您不能在对象内使用 console.log。