Skip to content

Commit

Permalink
return coroutine result in coroutine.Run (#122)
Browse files Browse the repository at this point in the history
This is useful in cases where the coroutine returns a value.
  • Loading branch information
achille-roussel authored Dec 8, 2023
2 parents b3b03b5 + 3c5825f commit a1ca737
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion coroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ type Context[R, S any] struct {

// Run executes a coroutine to completion, calling f for each value that the
// coroutine yields, and sending back each value that f returns.
func Run[R, S any](c Coroutine[R, S], f func(R) S) {
//
// If c was constructed with NewWithReturn, the return value of the coroutine is
// returned by Run. Otherwise, the zero-value is returned and can be ignored by
// the caller.
func Run[R, S any](c Coroutine[R, S], f func(R) S) R {
// The coroutine is run to completion, but f might panic in which case we
// don't want to leave it in an uncompleted state and interrupt it instead.
defer func() {
Expand All @@ -86,6 +90,8 @@ func Run[R, S any](c Coroutine[R, S], f func(R) S) {
s := f(r)
c.Send(s)
}

return c.Result()
}

// Yield sends v to the generator and pauses the execution of the coroutine
Expand Down

0 comments on commit a1ca737

Please sign in to comment.