loop

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func OnTick

func OnTick[TickType any](ticks iter.Seq[TickType], task func(context.Context, TickType) error) error

OnTick calls task on every tick from the ticker. The function returns the last task error when the ticker is stopped, or task fails with [ErrStopped].

Example

This example runs a loop, controlled by a periodic ticker. The called function is wrapped with a logger, that logs on every invocation and on error.

ticker := ticker.NewTimer(time.Second)
time.AfterFunc(2500*time.Millisecond, ticker.Stop)
startTime := time.Now()

// The tick function returns temporary error.
err := OnTick(ticker.Ticks(),
	func(_ context.Context, tick time.Time) error {
		fmt.Println("tick", tick.Sub(startTime).Round(time.Second))
		return fmt.Errorf("oops")
	})

fmt.Println(err)
Output:

tick 0s
tick 1s
tick 2s
oops

Types

This section is empty.

Jump to

Keyboard shortcuts

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