thread

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

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 AsyncContext added in v0.2.6

func AsyncContext[T any](ctx context.Context, f func() T) <-chan T

AsyncContext 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. If the context is done, the function will not be executed. The function will not be executed if the context is done.

func AsyncContextOrErr added in v0.2.6

func AsyncContextOrErr[T any](ctx context.Context, f func(context.Context) (T, error)) (<-chan T, <-chan error)

AsyncContextOrErr 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. If the context is done, the function will not be executed. The function will not be executed if the context is done.

func AsyncOrErr

func AsyncOrErr[T any](f func() (T, error)) (<-chan T, <-chan error)

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 WaitContext added in v0.2.6

func WaitContext[T any](ctx context.Context, v <-chan T) (T, error)

WaitContext 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.

func WaitContextOrErr added in v0.2.6

func WaitContextOrErr[T any](ctx context.Context, v <-chan T, err <-chan error) (T, error)

WaitContextOrErr 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 WaitOrErr

func WaitOrErr[T any](v <-chan T, err <-chan error) (T, error)

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.

Types

type Promise

type Promise[T any] interface {
	Resolve(T)
	Reject(error)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL