Skip to content

Instantly share code, notes, and snippets.

@alidevhere
Created October 18, 2022 13:30
Show Gist options
  • Save alidevhere/d93cfc35fb585f18d74d81943cfd6da6 to your computer and use it in GitHub Desktop.
Save alidevhere/d93cfc35fb585f18d74d81943cfd6da6 to your computer and use it in GitHub Desktop.
// 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