Skip to content

Commit 13a972c

Browse files
committed
doc: add examples.
1 parent f62ec8d commit 13a972c

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

retry_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -136,3 +137,23 @@ func TestRetryWithContext(t *testing.T) {
136137
a.NilNow(err)
137138
a.EqualNow(out, []any{1, nil})
138139
}
140+
141+
func ExampleRetry() {
142+
i := 0
143+
144+
out, err := Retry(func() error {
145+
i++
146+
if i != 3 {
147+
return errors.New("i != 3")
148+
} else {
149+
return nil
150+
}
151+
})
152+
fmt.Println(i)
153+
fmt.Println(out)
154+
fmt.Println(err)
155+
// Outputs:
156+
// 3
157+
// [<nil>]
158+
// <nil>
159+
}

until_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -108,3 +109,21 @@ func TestUntilWithContext(t *testing.T) {
108109
a.GteNow(dur, 100*time.Millisecond)
109110
a.LteNow(dur, 150*time.Millisecond)
110111
}
112+
113+
func ExampleUntil() {
114+
i := 0
115+
116+
out, err := Until(func(n int) bool {
117+
return n < 3
118+
}, func() int {
119+
i++
120+
return i
121+
})
122+
fmt.Println(i)
123+
fmt.Println(out)
124+
fmt.Println(err)
125+
// Outputs:
126+
// 3
127+
// [3]
128+
// <nil>
129+
}

while_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package async
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"testing"
78
"time"
89

@@ -98,3 +99,20 @@ func TestWhileWithContext(t *testing.T) {
9899
a.GteNow(dur, 100*time.Millisecond)
99100
a.LteNow(dur, 150*time.Millisecond)
100101
}
102+
103+
func ExampleWhile() {
104+
i := 0
105+
106+
out, err := While(func() bool {
107+
return i < 3
108+
}, func() {
109+
i++
110+
})
111+
fmt.Println(i)
112+
fmt.Println(out)
113+
fmt.Println(err)
114+
// Outputs:
115+
// 3
116+
// []
117+
// <nil>
118+
}

0 commit comments

Comments
 (0)