Documentation
¶
Overview ¶
Package promise provides a lightweight, generic Promise<T> implementation for asynchronous operations in the Kite terminal UI engine.
Promise ¶
Promise[T any] represents the eventual completion or failure of an asynchronous operation. It is created via New(), which runs the executor on a background scheduler. The scheduler (set via SetScheduler) controls background execution, microtask queues, and macrotask queues.
Methods ¶
- Then(fn, err) registers callbacks for fulfillment and rejection.
- Catch(err) registers only a rejection handler.
- Finally(fn) registers a handler for both settle paths.
- Await(ctx) blocks until the promise settles or the context is cancelled.
Factory Functions ¶
- Resolved[T](val) returns an immediately fulfilled promise.
- RejectedPromise[T](err) returns an immediately rejected promise.
Scheduler ¶
The Scheduler interface controls where and how executor functions run. A global scheduler can be set via SetScheduler; if none is set, promises fall back to a simple goroutine and synchronous dispatch.
Example:
p := promise.New(func(ctx context.Context) (string, error) {
return "hello", nil
})
p.Then(func(s string) {
fmt.Println(s)
}, nil)
Index ¶
- func ClearSchedulerIf(s Scheduler) bool
- func SetScheduler(s Scheduler)
- func Spawn(task func(ctx context.Context)) bool
- func Wrap[T any](fn func(context.Context) (T, error)) func(context.Context) *Promise[T]
- func WrapWithProps[P any, T any](fn func(context.Context, P) (T, error)) func(context.Context, P) *Promise[T]
- type Promise
- type Scheduler
- type State
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClearSchedulerIf ¶
ClearSchedulerIf clears the global scheduler to nil if and only if it currently matches s. Returns true if the global scheduler was cleared.
func SetScheduler ¶
func SetScheduler(s Scheduler)
SetScheduler sets the global scheduler used by all promises.
func Spawn ¶
Spawn launches a long-lived or unbounded task outside the bounded worker pool via the active global scheduler, binding its context and shutdown lifecycle to that scheduler. Returns false if task is nil or the scheduler is already stopped. When no scheduler is registered, Spawn runs task in a new goroutine with context.Background() and returns true.
func Wrap ¶
Wrap converts a synchronous function that takes a context into an asynchronous function that returns a Promise. The context provided when calling the returned function supplies its values, deadline, and cancellation. Scheduler shutdown also cancels the execution context. The wrapped function must honor cancellation.
func WrapWithProps ¶
func WrapWithProps[P any, T any](fn func(context.Context, P) (T, error)) func(context.Context, P) *Promise[T]
WrapWithProps converts a synchronous function that takes a context and a properties payload into an asynchronous function that returns a Promise. It preserves the caller's context and scheduler cancellation in the same way as Wrap.
Types ¶
type Promise ¶
type Promise[T any] struct { // contains filtered or unexported fields }
Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
func New ¶
New creates a new Promise and executes the executor in the background. The context provided to the executor is managed by the scheduler.
Example ¶
package main
import (
"context"
"fmt"
"strconv"
"github.com/masterkeysrd/kite/promise"
)
func main() {
port := promise.New(func(context.Context) (int, error) {
return strconv.Atoi("8080")
})
value, err := port.Await(context.Background())
if err != nil {
fmt.Println(err)
return
}
fmt.Println("port:", value)
}
Output: port: 8080
func RejectedPromise ¶
Rejected returns a new Promise that is already rejected with the given error.
func (*Promise[T]) Await ¶
Await blocks the caller until the promise is settled or the context is cancelled.
type Scheduler ¶
type Scheduler interface {
// RunBackground executes a task on a background worker pool.
// The provided context is managed by the scheduler.
RunBackground(task func(ctx context.Context))
// Spawn launches a long-lived or unbounded task in its own goroutine outside
// the fixed-size worker pool, while binding its context and shutdown lifecycle
// to the scheduler.
Spawn(task func(ctx context.Context))
// QueueMicrotask schedules a task to run as a microtask on the main thread.
QueueMicrotask(task func())
// QueueMacrotask schedules a task to run as a macrotask on the main thread.
QueueMacrotask(task func())
}