Skip to content

Commit

Permalink
try afterBoth
Browse files Browse the repository at this point in the history
  • Loading branch information
haitch committed Jun 26, 2022
1 parent 22f8251 commit 6388b30
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions after_both.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package asynctask

import "context"

// AfterBoth is a function that has 2 input.
type AfterBothFunc[T, S, R any] func(context.Context, *T, *S) (*R, error)

// AfterBoth runs the function after both 2 input task finished, and will be feed with result from 2 input task.
func AfterBoth[T, S, R any](ctx context.Context, tskT *Task[T], tskS *Task[S], next AfterBothFunc[T, S, R]) *Task[R] {
return Start(ctx, func(fCtx context.Context) (*R, error) {
t, err := tskT.Result(fCtx)
if err != nil {
return nil, err
}

s, err := tskS.Result(fCtx)
if err != nil {
return nil, err
}

return next(fCtx, t, s)
})
}

0 comments on commit 6388b30

Please sign in to comment.