-
-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:samber/lo
- Loading branch information
Showing
3 changed files
with
210 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,70 @@ | ||
package lo | ||
|
||
// Async executes a function in a goroutine and returns the result in a channel. | ||
func Async[T any](f func() T) chan T { | ||
ch := make(chan T) | ||
func Async[A any](f func() A) chan A { | ||
ch := make(chan A) | ||
go func() { | ||
ch <- f() | ||
}() | ||
return ch | ||
} | ||
|
||
// Async0 executes a function in a goroutine and returns a channel set once the function finishes. | ||
func Async0(f func()) chan struct{} { | ||
ch := make(chan struct{}) | ||
go func() { | ||
f() | ||
ch <- struct{}{} | ||
}() | ||
return ch | ||
} | ||
|
||
// Async1 is an alias to Async. | ||
func Async1[A any](f func() A) chan A { | ||
return Async(f) | ||
} | ||
|
||
// Async2 has the same behavior as Async, but returns the 2 results as a tuple inside the channel. | ||
func Async2[A any, B any](f func() (A, B)) chan Tuple2[A, B] { | ||
ch := make(chan Tuple2[A, B]) | ||
go func() { | ||
ch <- T2(f()) | ||
}() | ||
return ch | ||
} | ||
|
||
// Async3 has the same behavior as Async, but returns the 3 results as a tuple inside the channel. | ||
func Async3[A any, B any, C any](f func() (A, B, C)) chan Tuple3[A, B, C] { | ||
ch := make(chan Tuple3[A, B, C]) | ||
go func() { | ||
ch <- T3(f()) | ||
}() | ||
return ch | ||
} | ||
|
||
// Async4 has the same behavior as Async, but returns the 4 results as a tuple inside the channel. | ||
func Async4[A any, B any, C any, D any](f func() (A, B, C, D)) chan Tuple4[A, B, C, D] { | ||
ch := make(chan Tuple4[A, B, C, D]) | ||
go func() { | ||
ch <- T4(f()) | ||
}() | ||
return ch | ||
} | ||
|
||
// Async5 has the same behavior as Async, but returns the 5 results as a tuple inside the channel. | ||
func Async5[A any, B any, C any, D any, E any](f func() (A, B, C, D, E)) chan Tuple5[A, B, C, D, E] { | ||
ch := make(chan Tuple5[A, B, C, D, E]) | ||
go func() { | ||
ch <- T5(f()) | ||
}() | ||
return ch | ||
} | ||
|
||
// Async6 has the same behavior as Async, but returns the 6 results as a tuple inside the channel. | ||
func Async6[A any, B any, C any, D any, E any, F any](f func() (A, B, C, D, E, F)) chan Tuple6[A, B, C, D, E, F] { | ||
ch := make(chan Tuple6[A, B, C, D, E, F]) | ||
go func() { | ||
ch <- T6(f()) | ||
}() | ||
return ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters