Documentation ¶
Overview ¶
Package thread provides a simple way to run functions in goroutines with error handling.
Index ¶
- func Async[T any](f func() T) <-chan T
- func AsyncOrErr[T any](f func() (T, error)) (<-chan T, <-chan error)
- func Wait[T any](v <-chan T) T
- func WaitOrErr[T any](v <-chan T, err <-chan error) (T, error)
- func WaitTimeoutOrErr[T any](ctx context.Context, v <-chan T, err <-chan error) (T, error)
- func WaitWithContext[T any](ctx context.Context, v <-chan T) (T, error)
- type Caller
- type Promise
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Async ¶
func Async[T any](f func() T) <-chan T
Async runs the provided function in a separate goroutine and returns a channel that will receive the result of the function. The function fn will be executed asynchronously and its result will be sent to the channel. The channel will be closed after the result is sent.
func AsyncOrErr ¶
AsyncOrErr runs the provided function in a separate goroutine and returns two channels: one that will receive the result of the function, and another that will receive any error that occurs during the function execution. The function fn will be executed asynchronously and its result and error will be sent to the respective channels. The channels will be closed after the result or error is sent. If the provided function is nil, an error will be sent to the error channel.
func Wait ¶
func Wait[T any](v <-chan T) T
Wait waits for a value to be available on the provided channel.
The channel v must be a receive-only channel of type T. The function blocks until a value is received from the channel, and then returns that value.
Parameters:
- v: A receive-only channel of type T.
Returns:
- T: The value received from the channel.
func WaitOrErr ¶
WaitOrErr waits for a value to be available on the provided channel or an error to be available on the provided error channel.
The channel v must be a receive-only channel of type T. The function blocks until a value is received from the channel or an error is received from the error channel, and then returns the value or the error received from the channel.
Parameters:
- v: A receive-only channel of type T.
- err: A receive-only channel of type error.
Returns:
- T: The value received from the channel if successful.
- error: The error received from the error channel if successful.
func WaitTimeoutOrErr ¶
WaitTimeoutOrErr waits for a value to be available on the provided channel within the given context. If a value is received from the channel, it is returned along with a nil error. If an error is received from the error channel, it is returned along with the received error. If the context is done, the function returns the last received value and an error indicating the context is done.
Parameters:
- ctx: The context to control the waiting process.
- v: A receive-only channel of type T.
- err: A receive-only channel of type error.
Returns:
- T: The value received from the channel if successful.
- error: The error that caused the waiting process to be done.
func WaitWithContext ¶
WaitWithContext waits for a value to be available on the provided channel within the given context.
The channel v must be a receive-only channel of type T. The function blocks until a value is received from the channel or the context is done.
Parameters:
- ctx: The context to control the waiting process.
- v: A receive-only channel of type T.
Returns:
- T: The value received from the channel if successful.
- error: The error that caused the waiting process to be done.
Types ¶
type Caller ¶
type Caller[T any] interface { // Then sets a function to be called after the function call completes successfully. // The result of the function call is passed as an argument to the function. Then(func(T)) Caller[T] // Catch sets a function to be called if an error occurs during the function call. // The error is passed as an argument to the function. Catch(func(error)) Caller[T] // Finally sets a function to be called after the function call completes, regardless of whether an error occurred or not. Finally(func()) }
Caller is an interface that represents a function call that can be run in a goroutine. It allows for error handling and chaining of functions to be called after the function call completes.
func Try ¶
Try creates a new caller with the given function and runs it in a goroutine. The caller can be used to chain functions to be called after the function call completes.
func TryWithContext ¶
TryWithContext creates a new caller with the given function and context and runs it in a goroutine. The caller can be used to chain functions to be called after the function call completes.