Promise Package Methods and Functions
Resolve
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
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
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
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
Await is a proxy for AwaitWithContext
AwaitWithContext
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
Cancel calls the promises cancelFunc and closes the promises doneChan. This method can only be called once.
NewPanicError
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
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
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
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
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
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
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.