You canโt have this kind of background task.
Instead, you can wrap your API calls into a loop and triggers the desired requests based on some condition (doIf, switch, etc).
Iโm not sure if I understood you correctly but I think that this is possible.
In code where I try to create solution for token refresh, you can see code that is checking if itโs time for refresh token in loop (your background task) but main code doing own work.
Check this simulation:
Thank @slandelle , maybe I will try to persuade my colleague with your approach. @GeMi the task is kinda rush now so I will look into your approach later on, thank you for suggesting me.