# coroutines

Michael Friend

02/17/2020, 5:26 PM
Is there a way to implement dynamic timeout for a coroutine? Basically if I'm running callable A and B in parallel with some timeout t, I want to extend the timeout of callable A based on the response from callable B (which should be faster). Would I just have to avoid using withTimeout and instead keep track of the time and cancel manually?


02/18/2020, 6:22 AM
Can you run B with timeout tB and A with timeout tA, where tA is greater tB? If tB times out, you cancel job/deferred A. If tB doesn't time out, then A will continue to run until tA times out. If you want to time out A after a time depending on the response from B, then you could run B with timeout tB and run A without timeout. Then if B times out, you cancel A. If B returns, then you start a new coroutine with a delay for the remainder of time, after which A is cancelled. If A completes before this cancellation, then nothing happens.
👍 1

Michael Friend

02/18/2020, 3:50 PM
I think t hat will achieve what im looking for, thanks! The question came from a coworker so im gonna look into what makes the two coroutines so tightly tied to eachother