2016-07-22 8 views
0

이 같은 ComponentDidMount()에 내가 작업을 호출하고 구성 요소를 반응 특정 시간 이후 지속적으로 약속 발송 :reactjs 내에서

여기
componentDidMount() { 
     const { actions } = this.props 

     function save_project_continiously() { 
      console.log("inside") 
      actions.save_project().then(save_project_continiously) 
     } 

     save_project_continiously() 
    } 

내가 지속적으로 작업을 호출하고 .. 내 행동은 같은 것입니다 :

export function save_project() { 
    return (dispatch, getState) => { 

     setTimeout(() => { 
      return dispatch(manage_data()).then(response => { 
       console.log("Hellooww world") 
       return response 
      }) 
     }, 3000) 
    } 
} 
나는이 작업을 수행 할 때

은 내가

할 경우

.. 나에게 .then()ComponentDidMount()에서 함수가 아닙니다 말하는 오류를 제공

export function save_project() { 
     return (dispatch, getState) => { 

      return dispatch(manage_data()).then(response => { 
       console.log("Hellooww world") 
       return response 
      }) 
     } 
    } 

연속적으로 호출되지만 특정 시간 후에 계속해서 호출되기를 원합니다.

나는이 시도 :

export function save_project() { 
     return (dispatch, getState) => { 
      return new Promise((resolve, reject) => { 
       setTimeout(() => { 
        return dispatch(manage_data()).then(response => { 
         console.log("Hellooww world") 
         return response 
        }) 
       }, 3000) 
      }) 
     } 
    } 

을하지만, 한 번만 .. 오류를 제공하지 않습니다라고하지만, 그것은 단지 내가 continuosly Actions (동작)을 호출 할

내가 원하는 것은 .. 한 번 호출 행동을 완료 한 후 특정 시간 후에 여기

.. 나는 save_project를 호출 할과를 완료 한 후 다시 삼초 후 전화를 지속적에 가고 싶어

나는 그것이 일이 어떻게 할 수 있습니까 ??

제안 사항 사전

답변

1

을 시도,하지만 당신이 실제로하고 싶은 성공적인 약속 해결에 다시 코드를 실행합니다.

// Just return promise of dispatch instead of wrapping it in 
// a callback. 
export function save_project() { 
    return dispatch(manage_data()); 
} 

// Then use set timeout here 
function save_project_continiously() { 
    console.log("inside") 
    actions.save_project().then(() => { 
     setTimeout(save_project_continiously, 3000); 
    }); 
} 

또는 당신이 정말로 save_project에서 콜백을 원한다면, 당신은 그들이 어쨌든 undefined있어 당신의 예에서와 같이 적절한 인수 제대로 호출해야합니다

export function save_project() { 
    // So this code does return callback, so it does not return promise 
    // in your first example. 
    return (dispatch, getState) => { 
     // Then this code returns promise, but it never gets resolved. 
     return new Promise((resolve, reject) => { 
      setTimeout(() => { 
       // Then you return promise from set timeout, but it 
       // is doubtful as well 
       return dispatch(manage_data()).then(response => { 
        console.log("Hellooww world") 
        return response 
       }) 
      }, 3000) 
     }) 
    } 
} 

당신이 정말로 원하는 무엇입니까 .

0

에서

덕분에 당신이 약속에 약속을 포장하고,이 코드에서 setInterval()

export function save_project() { 
    return new Promise((resolve, reject) => { 
    setTimeout(() => { 
     dispatch(manage_data()).then(response => { 
     resolve(response); 
     }, error => { 
     reject(response); 
     }) 
    }, 3000) 
    }); 
} 
+0

3 초마다 호출됩니다. 그러나'manage_data'가 성공적으로 완료된 경우에만'save_project'가 3 초 후에 호출됩니다.이 작업은 – gamer

+0

내 대답을 업데이트했습니다. 이 방법이 효과가 없다면 유감 스럽지만 코드가 매우 혼란 스럽습니다. 'dispatch' 또는'manage_data()'가 무엇을하는지는 명확하지 않습니다. –