Answer the question
In order to leave comments, you need to log in
Delay sending a request, before receiving a response from the previous one?
Good afternoon, how to implement a delay in sending requests? React and NodeJS
At the moment, I made it so that the button has a state, for example, ready: true, if a request was sent, then the state changes to false, as soon as a response is received, the state changes back to true.
I would like to learn how to correctly and correctly implement this from more experienced developers.
Answer the question
In order to leave comments, you need to log in
you need to make a flag at the beginning of the request, for example isFetch and set it to true. If the request ended or some error occurred during the request, then set it to false. And if isFetch is true then don't execute the request.
_getPostcards () {
try {
this.setState({isFecth:true})
const url = domain + '?app_id='+ app_id +'&category='+ category +'&page=' + this.state.page
return fetch(url)
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson)
this.setState({ data: responseJson.photos, pageMax: responseJson.pages })
this.setState({isFecth:false}) //<--------
})
.catch((error) =>{
console.error(error);
this.setState({isFecth:false}) //<--------
});
} catch (e) {
throw e
this.setState({isFecth:false}) //<--------
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question