asyncutil

package
v0.1.821 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BlobZero = Blob{}

BlobZero is the reusable instance of Blob, equivalent to struct{}{}.

Functions

func DoWithTimeout

func DoWithTimeout[T any](
	ctx context.Context,
	timeout time.Duration,
	fn func(ctx context.Context) (T, error),
	set ...func(T),
) error

DoWithTimeout runs the provided function fn with a timeout.

If the operation completes within the given timeout, its result is optionally passed to the first function in setOptional (if provided).

If the context is cancelled or times out, a zero value is sent to the optional set function.

This utility is especially useful for wrapping asynchronous or IO-bound operations that require strict timeout control.

Example:

```go
var result string
err := asyncutil.DoWithTimeout[string](ctx, 2*time.Second, func(ctx context.Context) (string, error) {
    return "hello", nil
}, func(v string) {
    result = v
})
```

Example (without meaningful result):

```go
err := asyncutil.DoWithTimeout[asyncutil.Blob](ctx, 2*time.Second, func(ctx context.Context) (asyncutil.Blob, error) {
    return asyncutil.BlobZero, nil
})
```

Types

type Blob

type Blob struct{}

Blob is a named type used as a placeholder when a function returns no meaningful value.

Jump to

Keyboard shortcuts

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