Skip to content

Commit

Permalink
chore(docs): added playground links
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Mar 8, 2024
1 parent bf07e75 commit a04de99
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ go get github.com/yaitoo/async@latest
```

### Wait
wait all tasks to completed
wait all tasks to completed. [playground](https://go.dev/play/p/po8vX5ulSUi)

```
t := async.New[int](func(ctx context.Context) (int, error) {
Expand All @@ -50,7 +50,7 @@ if err == nil {


### WaitAny
wait any task to completed
wait any task to completed. [playground](https://go.dev/play/p/wfLtb2KDSsG)

```
t := async.New[int](func(ctx context.Context) (int, error) {
Expand All @@ -71,28 +71,31 @@ if err == nil {
```

### Timeout
cancel all tasks if it is timeout
cancel all tasks if it is timeout. [playground](https://go.dev/play/p/AY42qZQPQAI)
```
t := async.New[int](func(ctx context.Context) (int, error) {
time.Sleep(2 * time.Second)
t := async.New[int](func(ctx context.Context) (int, error) {
time.Sleep(2 * time.Second)
return 1, nil
}, func(ctx context.Context) (int, error) {
time.Sleep(2 * time.Second)
time.Sleep(2 * time.Second)
return 2, nil
})
result, err := t.WaitAny(context.Timeout(context.Background(), 1 * time.Second))
//result, err := t.Wait(context.Timeout(context.Background(), 1 * time.Second))
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
if err == nil {
fmt.Println(result)
}else{
fmt.Println(err) // context.DeadlineExceeded
}
result, err := t.WaitAny(ctx)
//result, err := t.Wait(ctx)
if err == nil {
fmt.Println(result)
} else {
fmt.Println(err) // context.DeadlineExceeded
}
```

### Cancel
manually cancel all tasks
manually cancel all tasks.[playground](https://go.dev/play/p/MxjTAZJKk16)

```
t := async.New[int](func(ctx context.Context) (int, error) {
Expand Down

0 comments on commit a04de99

Please sign in to comment.