Created
August 24, 2017 16:56
-
-
Save jonbodner/b7551067ced9718db116171ba5665f93 to your computer and use it in GitHub Desktop.
future-blog-post-17
This file contains hidden or 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
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