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 ¶
Click to show internal directories.
Click to hide internal directories.