Skip to content

Commit

Permalink
make the generic package exported
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Apr 30, 2022
1 parent b672a53 commit 26dd330
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions generic/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"github.com/reugn/async"
)

// asyncTask is a data type for controlling possibly lazy & asynchronous computations.
type asyncTask[T any] struct {
// AsyncTask is a data type for controlling possibly lazy & asynchronous computations.
type AsyncTask[T any] struct {
taskFunc func() (T, error)
}

func newAsyncTask[T any](taskFunc func() (T, error)) *asyncTask[T] {
return &asyncTask[T]{
// NewAsyncTask returns a new AsyncTask.
func NewAsyncTask[T any](taskFunc func() (T, error)) *AsyncTask[T] {
return &AsyncTask[T]{
taskFunc: taskFunc,
}
}

func (task *asyncTask[T]) call() async.Future {
// Call executes the AsyncTask and returns a Future.
func (task *AsyncTask[T]) Call() async.Future {
promise := async.NewPromise()
go func() {
res, err := task.taskFunc()
Expand Down
4 changes: 2 additions & 2 deletions generic/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
)

func TestAsyncTask(t *testing.T) {
task := newAsyncTask[string](func() (string, error) {
task := NewAsyncTask(func() (string, error) {
time.Sleep(1 * time.Second)
return "ok", nil
})
res, err := task.call().Get()
res, err := task.Call().Get()

internal.AssertEqual(t, "ok", res.(string))
internal.AssertEqual(t, err, nil)
Expand Down

0 comments on commit 26dd330

Please sign in to comment.