Skip to content

Commit 3009a2e

Browse files
committed
docs: fix readme typos.
1 parent 31e4313 commit 3009a2e

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

Diff for: README-CN.md

+16-15
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ go get -u github.com/ghosind/go-async
3232
`All`方法执行时,一旦有任意一个函数发生错误,它将立即结束并返回该错误,并且通过传递的上下文发生取消信号。
3333

3434
```go
35-
out, err := async.All(func (ctx context.Context) (int, error)) {
35+
out, err := async.All(func (ctx context.Context) (int, error) {
3636
return 0, nil
3737
}, func (ctx context.Context) (string, error)) {
3838
return "hello", nil
3939
}/*, ...*/)
40-
// out: [][]any{{0, nil}, {"hello", nil}}
40+
// out: [][]any{{0, <nil>}, {"hello", <nil>}}
4141
// err: <nil>
4242

43-
out, err := async.All(func (ctx context.Context) (int, error)) {
43+
out, err := async.All(func (ctx context.Context) (int, error) {
4444
return 0, nil
4545
}, func (ctx context.Context) (string, error)) {
4646
return "", errors.New("some error")
4747
}/*, ...*/)
48-
// out: nil
49-
// err: some error
48+
// out: [][]any{{}, {"", some error}}
49+
// err: function 1 error: some error
5050
```
5151

5252
若在执行过程中,即使有某个函数发生错误也不希望结束整体的运行,可以使用`AllCompleted`方法。`AllCompleted`方法会等待所有函数都执行完成或发生错误后,才会结束并返回所有函数的执行结果。
5353

5454
```go
55-
out, err := async.All(func (ctx context.Context) (int, error) {
55+
out, err := async.AllCompleted(func (ctx context.Context) (int, error) {
5656
return 0, nil
5757
}, func (ctx context.Context) (string, error) {
5858
return "", errors.New("some error")
@@ -80,10 +80,10 @@ out, index, err := async.Race(func (ctx context.Context) (int, error) {
8080
return "test", nil
8181
})
8282
// 第一个函数比第二个函数快的情况下:
83-
// out: []any{0, nil}, index: 0, err: nil
83+
// out: []any{0, <nil>}, index: 0, err: <nil>
8484
//
8585
// 第二个函数比第二个函数快的情况下:
86-
// out: []any{"test", nil}, index: 1, err: nil
86+
// out: []any{"test", <nil>}, index: 1, err: <nil>
8787
```
8888

8989
### 在并发限制下运行
@@ -102,8 +102,8 @@ out, err := async.Parallel(2, func (ctx context.Context) (int, error) {
102102
// Do something
103103
return nil
104104
}/* , ... */)
105-
// out: [][]any{{1, nil}, {"hello", nil}, {nil}}
106-
// err: nil
105+
// out: [][]any{{1, <nil>}, {"hello", <nil>}, {<nil>}}
106+
// err: <nil>
107107
```
108108

109109
在某一个函数发生错误时,`Parallel`也会结束并发送取消信号至其它函数。若不希望结束其它函数的执行,可以使用`ParallelCompleted`代替,它将执行全部函数直到所有函数都完成或发生错误。
@@ -113,22 +113,23 @@ out, err := async.Parallel(2, func (ctx context.Context) (int, error) {
113113
通过`Forever`方法,可以持续运行某一函数直到该函数发生错误。`Forever`方法需要接受一个`ForeverFn`类型的方法用于执行,对于该类型的具体信息可以参考示例下方的描述。
114114

115115
```go
116-
err := async.Forever(func(ctx context.Context, next func(context.Context)) error {
117-
v, ok := ctx.Value("key")
118-
if ok {
116+
err := Forever(func(ctx context.Context, next func(context.Context)) error {
117+
v := ctx.Value("key")
118+
if v != nil {
119119
vi := v.(int)
120120
if vi == 5 {
121121
return errors.New("finish")
122122
}
123123

124124
fmt.Printf("value: %d\n", vi)
125125

126-
next(context.WithValue(ctx, "key", vi + 1))
126+
next(context.WithValue(ctx, "key", vi+1))
127127
} else {
128128
next(context.WithValue(ctx, "key", 1))
129129
}
130+
131+
return nil
130132
})
131-
fmt.Printf("err: %v\n", err)
132133
// value: 1
133134
// value: 2
134135
// value: 3

Diff for: README.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,32 @@ The most of utility functions of this library accept any type of function to run
3232
If any function returns an error or panics, the `All` function will terminate immediately and return the error. It'll also send a cancel signal to other uncompleted functions by context if they accept a context by their first parameter.
3333

3434
```go
35-
out, err := async.All(func (ctx context.Context) (int, error)) {
35+
out, err := async.All(func (ctx context.Context) (int, error) {
3636
return 0, nil
3737
}, func (ctx context.Context) (string, error)) {
3838
return "hello", nil
3939
}/*, ...*/)
40-
// out: [][]any{{0, nil}, {"hello", nil}}
40+
// out: [][]any{{0, <nil>}, {"hello", <nil>}}
4141
// err: <nil>
4242

43-
out, err := async.All(func (ctx context.Context) (int, error)) {
43+
out, err := async.All(func (ctx context.Context) (int, error) {
4444
return 0, nil
4545
}, func (ctx context.Context) (string, error)) {
4646
return "", errors.New("some error")
4747
}/*, ...*/)
48-
// out: nil
49-
// err: some error
48+
// out: [][]any{{}, {"", some error}}
49+
// err: function 1 error: some error
5050
```
5151

5252
If you do not want to terminate the execution when some function returns an error or panic, you can try the `AllCompleted` function. The `AllCompleted` function executes until all functions are finished or panic. It'll return a list of the function return values, and an error to indicate whether any functions return error or panic.
5353

5454
```go
55-
out, err := async.All(func (ctx context.Context) (int, error) {
55+
out, err := async.AllCompleted(func (ctx context.Context) (int, error) {
5656
return 0, nil
5757
}, func (ctx context.Context) (string, error) {
5858
return "", errors.New("some error")
5959
}/*, ...*/)
60-
// out: [][]any{{0, nil}, {"", some error}}}
60+
// out: [][]any{{0, <nil>}, {"", some error}}}
6161
// err: function 1 error: some error
6262
```
6363

@@ -80,10 +80,10 @@ out, index, err := async.Race(func (ctx context.Context) (int, error) {
8080
return "test", nil
8181
})
8282
// If the first function faster than the second one:
83-
// out: []any{0, nil}, index: 0, err: nil
83+
// out: []any{0, <nil>}, index: 0, err: <nil>
8484
//
8585
// Otherwise:
86-
// out: []any{"test", nil}, index: 1, err: nil
86+
// out: []any{"test", <nil>}, index: 1, err: <nil>
8787
```
8888

8989
### Run all functions with concurrency limit
@@ -102,8 +102,8 @@ out, err := async.Parallel(2, func (ctx context.Context) (int, error) {
102102
// Do something
103103
return nil
104104
}/* , ... */)
105-
// out: [][]any{{1, nil}, {"hello", nil}, {nil}}
106-
// err: nil
105+
// out: [][]any{{1, <nil>}, {"hello", <nil>}, {<nil>}}
106+
// err: <nil>
107107
```
108108

109109
The `Parallel` will also be terminated if any function panics or returns an error. If you do not want to terminate the execution of other functions, you can try to use `ParallelCompleted`. The `ParallelCompleted` function will run all functions until all functions are finished. It will return the output list and an error to indicate whether any function errored.
@@ -113,20 +113,22 @@ The `Parallel` will also be terminated if any function panics or returns an erro
113113
For `Forever` function, you can use it to run a function forever until it returns an error or panics. You need to run the `Forever` function with a `ForeverFn` type function, and you can see more information about `ForeverFn` after the following example.
114114

115115
```go
116-
err := async.Forever(func(ctx context.Context, next func(context.Context)) error {
117-
v, ok := ctx.Value("key")
118-
if ok {
116+
err := Forever(func(ctx context.Context, next func(context.Context)) error {
117+
v := ctx.Value("key")
118+
if v != nil {
119119
vi := v.(int)
120120
if vi == 5 {
121121
return errors.New("finish")
122122
}
123123

124124
fmt.Printf("value: %d\n", vi)
125125

126-
next(context.WithValue(ctx, "key", vi + 1))
126+
next(context.WithValue(ctx, "key", vi+1))
127127
} else {
128128
next(context.WithValue(ctx, "key", 1))
129129
}
130+
131+
return nil
130132
})
131133
fmt.Printf("err: %v\n", err)
132134
// value: 1

0 commit comments

Comments
 (0)