Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created August 24, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbodner/b7551067ced9718db116171ba5665f93 to your computer and use it in GitHub Desktop.
Save jonbodner/b7551067ced9718db116171ba5665f93 to your computer and use it in GitHub Desktop.
future-blog-post-17
func NewWithContext(ctx context.Context, inFunc Process) Interface {
f := New(inFunc).(*futureImpl)
c := ctx.Done()
if c != nil {
go func() {
select {
case <-c:
//if context is cancelled, cancel future
f.Cancel()
case <-f.cancel:
//do nothing, cancelled future doesn’t cancel context
case <-f.done:
//do nothing, done future doesn’t cancel context
}
}()
}
return f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment