Product Help

Promise Package Methods and Functions

Resolve

func (p *promiseImpl[T]) Resolve(result T)

Resolve sets the result in the promise then closes the doneChan. A promise can only call this method once.

Parameters:

  • result: Value to set as the result for the promise.

Reject

func (p *promiseImpl[T]) Reject(err error)

Reject sets the error field to err, marking that the promise had an error. A promise can only call this method once.

Parameters:

  • err: The error to set in the promise struct.

Then

func (p *promiseImpl[T]) Then(successFunc func(T) interface{}) Promise[interface{}]

Then takes in a successFunc and waits to read from the doneChan of the promise. If there are no errors the successFunc is called which can return another promise.

Parameters:

  • successFunc: Will be called when the promise is done.

Returns:

  • Promise[interface{}]: A promise.

Catch

func (p *promiseImpl[T]) Catch(errorFunc func(error) interface{}) Promise[interface{}]

Catch takes an errorFunc and waits for the promise to be completed and then calls errorFunc, and returns another Promise.

Parameters:

  • errorFunc: The error function to be called once the promise is done.

Returns:

  • Promise[interface{}]: Another promise.

Await

func (p *promiseImpl[T]) Await() (T, error)

Await is a proxy for AwaitWithContext

AwaitWithContext

func (p *promiseImpl[T]) AwaitWithContext(ctx context.Context) (T, error)

AwaitWithContext waits till one chanel is ready to be read from and then returns result and error. Can read from p.doneChan, ctx.Done(), p.ctx.Done().

Parameters:

  • ctx: The context to read from.

Returns:

  • T: The result field in promise.

  • error: Any errors from the operation.

Cancel

func (p *promiseImpl[T]) Cancel()

Cancel calls the promises cancelFunc and closes the promises doneChan. This method can only be called once.

NewPanicError

func NewPanicError(recovered interface{}) PanicError

NewPanicError creates a new instance of PanicError.

Parameters:

  • recovered: The value to set Recovered to in the PanicError struct.

Returns:

  • PanicError: The new PanicError Instance.

All

func All[T any](promises []Promise[T]) Promise[[]T]

All waits for all promises to resolve and returns their results. If any promise rejects, All rejects immediately with that error.

Parameters:

  • promises: A slice of Promises.

Returns:

  • Promise[[]T]: A Promise of type []T

Race

func Race[T any](promises []Promise[T]) Promise[T]

Race returns a promise that resolves or rejects as soon as one of the input promises resolves or rejects.

Parameters:

  • promises: A slice of Promises.

Returns:

  • Promise[t]: A Promise.

NewPromiseWithContext

func NewPromiseWithContext[T any](ctx context.Context) Promise[T]

NewPromiseWithContext creates a new Promise with a provided context.

Parameters:

  • ctx: Context that will be used in the promise.

Returns:

  • Promise[T]: A Promise with the specified context.

WithCancel

func WithCancel[T any](executor func(Promise[T])) Promise[T]

WithCancel Cancellation is already partially handled in the core promise.go file. Additional cancellation utilities can be added here if needed.

Parameters:

  • executor: Runs async once passed in.

Returns:

  • Promise[T]: A new Promise.

AwaitWithTimeout

func (p *promiseImpl[T]) AwaitWithTimeout(timeout time.Duration) (T, error)

AwaitWithTimeout waits for the promise to resolve or reject within the specified timeout.

Parameters:

  • timeout: A duration to wait.

Returns:

  • T: The result after waiting for the Promise to be resolved.

Timeout

func Timeout[T any](duration time.Duration, err error) Promise[T]

Timeout creates a promise that rejects after the specified duration.

Parameters:

  • duration: The duration to timeout.

  • err: The error that will be passed when rejecting the Promise.

Returns:

  • Promise[T]: A new Promise.

Last modified: 28 January 2025