簡體   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