Created
October 18, 2022 13:30
-
-
Save alidevhere/d93cfc35fb585f18d74d81943cfd6da6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sleep function is updated as below | |
func sleepForWait(ctx context.Context,t time.Duration) { | |
select { | |
case <-ctx.Done(): | |
return | |
case <-time.After(t): | |
return | |
} | |
} | |
//We will use context to cancel the sleeping process | |
ctx, cancel := context.WithCancel(context.Background()) | |
//Now we can just call it to wait for 5 min | |
sleepForWait(ctx,5 * time.Minute) | |
// We will save this function in a variable and when user hits stop we will call this functions | |
// it will close the channel and triger ctx.Done() case to execute, Hence return | |
cancel() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment