reactjs 在特定时间后连续发送承诺
reactjs dispatch promise continuously after specific time
在我的 React 组件中,我在 ComponentDidMount()
中调用一个动作,如下所示:
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)
})
}
}
但是它只调用了一次..没有给出任何错误但是它只调用了一次..
我想要的是我想在完成动作后的特定时间后连续调用动作。
在这里我想调用 save_project
完成后我想在 3 秒后再次调用它并继续..
我怎样才能实现它??
有什么建议吗??
提前致谢
尝试setInterval()
export function save_project() {
return new Promise((resolve, reject) => {
setTimeout(() => {
dispatch(manage_data()).then(response => {
resolve(response);
}, error => {
reject(response);
})
}, 3000)
});
}
在此代码中,您将 promise 包装在 promise 中,但您实际想要做的是在成功解决 promise 时再次执行此代码。
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)
})
}
}
你真正想做的是:
// 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
。
在我的 React 组件中,我在 ComponentDidMount()
中调用一个动作,如下所示:
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)
})
}
}
但是它只调用了一次..没有给出任何错误但是它只调用了一次..
我想要的是我想在完成动作后的特定时间后连续调用动作。
在这里我想调用 save_project
完成后我想在 3 秒后再次调用它并继续..
我怎样才能实现它??
有什么建议吗??
提前致谢
尝试setInterval()
export function save_project() {
return new Promise((resolve, reject) => {
setTimeout(() => {
dispatch(manage_data()).then(response => {
resolve(response);
}, error => {
reject(response);
})
}, 3000)
});
}
在此代码中,您将 promise 包装在 promise 中,但您实际想要做的是在成功解决 promise 时再次执行此代码。
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)
})
}
}
你真正想做的是:
// 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
。