Skip to content

Commit 6388b30

Browse files
author
Haitao Chen
committed
try afterBoth
1 parent 22f8251 commit 6388b30

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

after_both.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package asynctask
2+
3+
import "context"
4+
5+
// AfterBoth is a function that has 2 input.
6+
type AfterBothFunc[T, S, R any] func(context.Context, *T, *S) (*R, error)
7+
8+
// AfterBoth runs the function after both 2 input task finished, and will be feed with result from 2 input task.
9+
func AfterBoth[T, S, R any](ctx context.Context, tskT *Task[T], tskS *Task[S], next AfterBothFunc[T, S, R]) *Task[R] {
10+
return Start(ctx, func(fCtx context.Context) (*R, error) {
11+
t, err := tskT.Result(fCtx)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
s, err := tskS.Result(fCtx)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
return next(fCtx, t, s)
22+
})
23+
}

0 commit comments

Comments
 (0)