Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PromiseState ¶
type PromiseState int
type PromiseStruct ¶
type PromiseStruct struct {
// contains filtered or unexported fields
}
func Promise ¶
func Promise(executor func(resolve func(interface{}), reject func(error))) *PromiseStruct
Promise creates and returns a new Promise object that represents the eventual completion (or failure) of an asynchronous operation, and executes the specified executor function immediately. The executor function takes two arguments, a resolve function and a reject function, that allow the promise to be resolved with a value or rejected with a reason, respectively. The Promise object returned by this function has methods `Then`, `Catch`, and `Finally` that allow for chaining and handling of the eventual fulfillment or rejection of the promise. The Promise object also provides a blocking `Await` method that can be used to wait for the promise to be resolved or rejected.
func (*PromiseStruct) Await ¶
func (promise *PromiseStruct) Await() (interface{}, error)
Await blocks the execution of the program until the promise resolves or rejects, and returns either the resolved value or an error. It returns an error only if the promise was rejected, and the resolved value otherwise.
func (*PromiseStruct) Catch ¶
func (p *PromiseStruct) Catch(fn func(error) interface{}) *PromiseStruct
Catch registers a callback function to be called when the promise is rejected. If the promise is already rejected, the callback is called immediately with the rejection reason. If the promise is resolved, the callback is skipped. The callback function takes one argument, the rejection reason of the promise, and should return a value or a new promise that will be resolved with that value. Returns a new promise that is resolved with the return value of the callback function or rejected with the same reason as the original promise, if the callback function throws an error.
func (*PromiseStruct) Finally ¶
func (promise *PromiseStruct) Finally(fn func()) *PromiseStruct
Finally registers a callback function to be called when the promise is either resolved or rejected. If the promise is already resolved or rejected, the callback is called immediately. The callback function takes no arguments and should not return anything. Returns the same promise instance to allow for chaining of methods.
func (*PromiseStruct) Then ¶
func (p *PromiseStruct) Then(fn func(interface{}) interface{}) *PromiseStruct
Then registers a callback function to be called when the promise is resolved. If the promise is already resolved, the callback is called immediately with the resolved value. If the promise is rejected, the callback is skipped. The callback function takes one argument, the resolved value of the promise, and should return a value or a new promise that will be resolved with that value. Returns a new promise that is resolved with the return value of the callback function or rejected with the same reason as the original promise, if the callback function throws an error.