This is a Go module that provides an implementation of Promises, similar to those in JavaScript, including support for then, catch, and finally. It also allows for Await to be used to block the execution of the program until the promise resolves or rejects.
To use this module in your Go project, run the following command:
go get github.com/goify/asyncify
This Go module provides a simple implementation of Promises in JavaScript with support for then
, catch
, and finally
handlers.
Here's an example of how to use the promise type with then
, catch
, and finally
:
package main
import "github.com/goify/asyncify"
func main() {
promise := asyncify.Promise(func(resolve func(interface{}), reject func(error)) {
// Do some asynchronous operation here
time.Sleep(2 * time.Second)
if true {
resolve("Success!")
} else {
reject(errors.New("Error!"))
}
})
promise.Then(func(result interface{}) interface{} {
// Handle fulfilled promise
fmt.Println(result)
return "Done!"
}).Catch(func(err error) interface{} {
// Handle rejected promise
fmt.Println(err.Error())
return "Done!"
}).Finally(func() {
// Handle either case
fmt.Println("Finished!")
})
}
In this example, a new promise
is created with an asynchronous operation that takes 2 seconds to complete. The then
handler is used to handle the successful resolution of the promise, the catch
handler is used to handle any errors that may occur, and the finally
handler is used to handle either case.
Here's an example of how to use the Await
method to block the execution of the program until the promise resolves or rejects:
package main
import "github.com/goify/asyncify"
func main() {
promise := asyncify.Promise(func(resolve func(interface{}), reject func(error)) {
// Do some asynchronous operation here
time.Sleep(2 * time.Second)
if true {
resolve("Success!")
} else {
reject(errors.New("Error!"))
}
})
result, err := promise.Await()
if err != nil {
// Handle error
fmt.Println(err.Error())
} else {
// Handle result
fmt.Println(result)
}
}
In this example, a new promise
is created with an asynchronous operation that takes 2 seconds to complete. The Await
method is used to block the execution of the program until the promise resolves or rejects. If the promise is resolved, the result is returned. If the promise is rejected, an error is returned.
The PromiseStruct
type represents a promise that will be resolved with a value or rejected with an error. It has the following methods:
Promise
creates a new promise with an executor function that takes two functions as arguments: resolve
and reject
. resolve
should be called with the result of the promise when it is successfully resolved, and reject
should be called with an error if the promise is rejected.
Then
creates a new promise that is resolved with the result of the fn
function when the original promise is fulfilled. If the original promise is rejected, the new promise is rejected with the same error.
Catch
creates a new promise that is resolved with the result of the fn
function when the original promise is rejected. If the original promise is fulfilled, the new promise is resolved with the same result.
Finally
creates a new promise
The Await
function is a blocking function that allows you to wait for a promise to resolve or reject. It waits until the promise state changes from pending
to either fulfilled
or rejected
. If the promise is fulfilled, Await
returns nil, and if the promise is rejected, it returns the error that caused the rejection.
go test
Asyncify is an MIT-licensed open source project. It can grow thanks to the sponsors and support.
Asyncify is MIT licensed.