diff --git a/task.go b/task.go index de48898..55bf894 100644 --- a/task.go +++ b/task.go @@ -8,6 +8,15 @@ import ( "time" ) +// ActionToFunc convert a Action to Func (C# term), to satisfy the AsyncFunc interface. +// Action is function that runs without return anything +// Func is function that runs and return something +func ActionToFunc(action func(context.Context) error) func(context.Context) (*interface{}, error) { + return func(ctx context.Context) (*interface{}, error) { + return nil, action(ctx) + } +} + // AsyncFunc is a function interface this asyncTask accepts. type AsyncFunc[T any] func(context.Context) (*T, error) diff --git a/task_test.go b/task_test.go index d21613c..ba6f7c5 100644 --- a/task_test.go +++ b/task_test.go @@ -150,6 +150,20 @@ func TestCompletedGenericTask(t *testing.T) { assert.Equal(t, *resultGet, result) } +func TestActionToFunc(t *testing.T) { + t.Parallel() + + action := func(ctx context.Context) error { + return nil + } + + ctx := context.Background() + task := asynctask.Start(ctx, asynctask.ActionToFunc(action)) + result, err := task.Result(ctx) + assert.NoError(t, err) + assert.Nil(t, result) +} + func TestCrazyCaseGeneric(t *testing.T) { t.Parallel() ctx, cancelFunc := newTestContextWithTimeout(t, 3*time.Second)