Skip to content

Commit 9b8233c

Browse files
committed
Initial commit
1 parent 1b84bfd commit 9b8233c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

async.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,35 @@
66
package async
77

88
type Task func() interface{}
9-
type Result chan interface{}
9+
type Channel chan interface{}
10+
type Result []interface{}
11+
type Out interface{}
1012

1113
type Promise struct {
1214
//TODO:
1315
//Context - Cancel, Timeout
1416
//Concurrency vs Parallelism
1517
//What to do on error? Is it best way to abort all operation?
18+
//Can I use map for tasks? It will be easy to associate results with keys
1619
}
1720

18-
func (p *Promise) All(tasks []Task) []Result {
19-
workers := make([]Result, 0, len(tasks))
21+
func (p *Promise) All(tasks []Task) Result {
22+
workers := make([]Channel, 0, len(tasks))
2023
for _, task := range tasks {
2124
result := execute(task)
2225
workers = append(workers, result)
2326
}
24-
return workers
27+
28+
// gather data from all channels
29+
out := make(Result, 0, len(tasks))
30+
for _, a := range workers {
31+
out = append(out, <-a)
32+
}
33+
return out
2534
}
2635

27-
func execute(task Task) Result {
28-
out := make(Result)
36+
func execute(task Task) Channel {
37+
out := make(Channel)
2938
{
3039
go func() {
3140
defer close(out)

examples/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ func main() {
2222
},
2323
}
2424

25-
for _, a := range promise.All(tasks) {
26-
// switch statement for results
27-
fmt.Println(<-a)
28-
}
25+
result := promise.All(tasks)
26+
27+
fmt.Println("Result: ", result)
2928

3029
fmt.Println("exit")
3130
}

0 commit comments

Comments
 (0)