1

I have to make a request to the backend server to retrieve some data, and inside the response comes the hour of the day that I have to make the request again. For example: I make the request at 12:00 am, and the response comes with the data I need, and also the next time I have to make the request again, for example "16:00".

This 'loop' will never end as long as the application is running. The thing is, my code works, but I dont know whether it is a good practice, because I call the same function over and over again.
Can someone explain to me if it's okay what I'm doing?

This is my code so far:

aFunction() { this.programacionService.getNextData().subscribe((r: any) => { console.log(r); window.setTimeout(() => { // Programming logic // Repeat the process this.aFunction.call(this); }, // This is a method to format the hour of the day this.functionTimeout(r.hourToMakeTheRequestAgain) ); }) } 

Is this correct? Won't it make the 'callStack' overflow over time? Thanks in advance!

5
  • If your worry is calling the function multiple times, there are many ways to prevent that Commented Jan 31, 2022 at 14:44
  • My problem is more 'i want to call a function over and over again, but do it correctly' than 'calling a function multiple times'. I NEED to call the function multiple times. Thanks for replying! Commented Jan 31, 2022 at 14:55
  • why do you subscribe to the getNextData() outside the setTimeOut() function? Won't this cause old data processing when it gets to the setTimeOut() function? Commented Jan 31, 2022 at 15:13
  • Hi! @TroudiGhaith im using Angular2. The subscription is to make the request to the backend server. Inside the response (r) there's the data I need, as well as the next time I have to make the request again. I first subscribe to the getNextData because that is what begins all the process of getting data over and over again Commented Jan 31, 2022 at 15:22
  • What I've been meaning, is that making the request inside the setTimeOut() would be better to fetch the last updates of the database, and of course, the request is going to be made again. Commented Jan 31, 2022 at 15:38

1 Answer 1

2

I don't know if this is true but : as long as your request would not take a lot of work from the server, you'll be fine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.