Skip to content

Commit 1b84bfd

Browse files
committed
Initial commit
1 parent 60c12c5 commit 1b84bfd

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@
1010

1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
13+
14+
# editors
15+
.idea
16+
.DS_Store
17+
.vscode

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Go Lang | REST Services
3+
Copyright (c) 2019 Roshan Gade
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# async
22
Perform multiple asynchronous tasks concurrently.
3+
4+
5+
# INCOMPLETE
6+
Working on it

async.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*!
2+
* go-rs/async
3+
* Copyright(c) 2019 Roshan Gade
4+
* MIT Licensed
5+
*/
6+
package async
7+
8+
type Task func() interface{}
9+
type Result chan interface{}
10+
11+
type Promise struct {
12+
//TODO:
13+
//Context - Cancel, Timeout
14+
//Concurrency vs Parallelism
15+
//What to do on error? Is it best way to abort all operation?
16+
}
17+
18+
func (p *Promise) All(tasks []Task) []Result {
19+
workers := make([]Result, 0, len(tasks))
20+
for _, task := range tasks {
21+
result := execute(task)
22+
workers = append(workers, result)
23+
}
24+
return workers
25+
}
26+
27+
func execute(task Task) Result {
28+
out := make(Result)
29+
{
30+
go func() {
31+
defer close(out)
32+
out <- task()
33+
}()
34+
}
35+
return out
36+
}

examples/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/go-rs/async"
8+
)
9+
10+
func main() {
11+
fmt.Println("start")
12+
var promise async.Promise
13+
14+
tasks := []async.Task{
15+
func() interface{} {
16+
time.Sleep(1000 * time.Microsecond)
17+
return "Hello"
18+
},
19+
func() interface{} {
20+
time.Sleep(100 * time.Microsecond)
21+
return "World"
22+
},
23+
}
24+
25+
for _, a := range promise.All(tasks) {
26+
// switch statement for results
27+
fmt.Println(<-a)
28+
}
29+
30+
fmt.Println("exit")
31+
}

0 commit comments

Comments
 (0)