ReactJS – How do I add SETTIMEOUT to get commitment to use REDUX?

If the cancellation of the submission has not been resolved for a period of time, I would like to display a timeout error to the user.

I have seen some of them by adding setTimeout here Good example:
https://github.com/github/fetch/issues/175

But how to deal with the acquisition of promises that use redux at the same time? For example.

export function getData() {
return (dispatch, getState) => {
fetch('blah.com/data')< br /> .then(response => response.json())
.then(json => dispatch(getDataSuccess(json)))
.catch(
error => {
console.log(error)
}
)
dispatch({
type: DATA_FETCH_REQUEST
})
}
}

Thank you for reading!

I have always longed for a reason to use Promise.race, which is suitable for this use case. Promise. Race waits for the first resolution or the first rejection. Therefore, if the rejection fires first, then it will never fire on Promise.race. More here https://developer.mozilla.org/en-US/docs/ Web/JavaScript/Reference/Global_Objects/Promise/race. Sorry, I have no chance to test the code.

export function getData() {
return (dispatch , getState) => {
let timeout = new Promise((resolve, reject) => {
setTimeout(reject, 300,'request timed out');
})
let fetch = new Promise((resolve, reject) => {
fetch('blah.com/data')
.then(response => response.json())
. then(json => resolve(json))
.catch(reject)
})
return Promise
.race([timeout, fetch])
.then (json => dispatch(getDataSuccess(json)))
.catch(err => dispatch(getDataTimeoutOrError(err)))
}
}

If the cancellation of the submission has not been resolved for a period of time, I want to display a timeout error to the user.

I have seen some good things by adding setTimeout here Example:
https://github.com/github/fetch/issues/175

But how to deal with the acquisition of promises that use redux at the same time? For example.

export function getData() {
return (dispatch, getState) => {
fetch('blah.com/data')< br /> .then(response => response.json())
.then(json => dispatch(getDataSuccess(json)))
.catch(
error => {
console.log(error)
}
)
dispatch({
type: DATA_FETCH_REQUEST
})
}
}

Thank you for reading!

I have always been eager to have a reason to use Promise.race, which is suitable for this use case. Promise.race waits for the first resolution or the first rejection. Therefore, if it refuses to fire first, then it will never fire on Promise.race. More here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race. Sorry, I have no chance to test the code.

export function getData() {
return (dispatch, getState) => {
let timeout = new Promise((resolve, reject) => {
setTimeout(reject, 300,'request timed out');
})
let fetch = new Promise((resolve, reject) => {
fetch('blah.com/data')
.then(response => response.json())
.then(json => resolve(json))
.catch(reject)
})
return Promise
.race([timeout, fetch])
.then(json => dispatch(getDataSuccess(json)))
.catch(err => dispatch(getDataTimeoutOrError(err)))
}
}

Leave a Comment

Your email address will not be published.