A promise represents the eventual result of an asynchronous operation
This implementation does not seek to comply with the specification, however, aims to be at least compatible but also faster and more asynchronous.
For example, the order of precedence is not guaranteed so as not to block anything unnecessary.
prom := promise.New(func (resolve promise.PromiseResolve, reject promise.PromiseReject) {
// note: synchronous as the top-level context
// ... do something ...
if (ok) {
resolve(value)
} else {
reject(err)
}
})
// ... do something ...
prom.Then(func (result any) (any, error) {
// note: asynchronous context
log.Print(result) // value
return nil, nil
})
You can also use Async
/Await
abstractions:
prom := async.Async(func () (any, error) {
// note: asynchronous context
return "hello from 2077"
})
// ... do something ...
result, _ := async.Await(prom)
log.Print(result) // hello from 2077
The creation of Promise is synchronous, so you can use the Deferred
abstraction,
which exposes Resolve
/Reject
functions:
def := deferred.New()
def.Then(func (result any) (any, error) {
// note: asynchronous context
log.Print(result) // wow
})
def.Resolve("wow")
If you need to handle panics, use PromisifyPanic
like this:
prom := panics.PromisifyPanic(func () {
// note: asynchronous context
// ... do something ...
panic("something went wrong")
})
// ... do something ...
prom.Catch(func (reason error) (any, error) {
// note: asynchronous context
log.Print(reason) // something went wrong
return nil, nil
})
go get -u github.com/eolme/go-promise
github.com/eolme/go-promise is MIT licensed.