Documentation
ΒΆ
Overview ΒΆ
Package promise is a utility for interacting with JavaScript promises.
All errors returned from this package (except for promise.Any) are of type promise.Reason which can be converted back to js.Value if necessary.
Example ΒΆ
// create a new Promise p, resolve, reject := promise.New() go func() { time.Sleep(100 * time.Millisecond) // do some asynchronous job... if err := error(nil); err != nil { reject(err) // reject promise if something went wrong return } // resolve promise if all looks good resolve("asynchronous job is done!") }() // wait for the promise to resolve or reject v, err := promise.Await(p) if err != nil { fmt.Printf("error: %#v\n", err.Error()) return } fmt.Println(v)
Output: asynchronous job is done!
Index ΒΆ
- Constants
- func All(ps []js.Value) ([]js.Value, error)
- func Any(ps []js.Value) (js.Value, error)
- func Await(p js.Value) (js.Value, error)
- func New() (p js.Value, resolve func(interface{}), reject func(interface{}))
- func Race(ps []js.Value) (js.Value, error)
- func Reject(v interface{}) js.Value
- func Resolve(v interface{}) js.Value
- type AggregateError
- type Reason
- type Result
Examples ΒΆ
Constants ΒΆ
const ( // Fulfilled is the state value of a resolved promise. Fulfilled = "fulfilled" // Rejected is the state value of a rejected promise. Rejected = "rejected" )
Variables ΒΆ
This section is empty.
Functions ΒΆ
func All ΒΆ added in v1.1.0
All waits for a given list of promises to be resolved and returns a list of the results.
It returns an error if any of the input promises rejects.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
Example ΒΆ
values, err := promise.All([]js.Value{ promise.Resolve(1), promise.Resolve(2), promise.Resolve(3), }) if err != nil { fmt.Printf("error: %#v\n", err.Error()) return } for _, v := range values { fmt.Println(v.Int()) }
Output: 1 2 3
func Any ΒΆ added in v1.1.0
Any returns the result of the first fulfilled promise in a given list of promises.
If no promises in the given list fulfills, then it returns a promise.AggregateError.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
Example ΒΆ
p1 := promise.Reject("rejected at first!") p2, resolve2, _ := promise.New() p3, _, reject3 := promise.New() time.AfterFunc(200*time.Millisecond, func() { resolve2("resolved at last!") }) time.AfterFunc(100*time.Millisecond, func() { reject3("eventually rejected!") }) v, err := promise.Any([]js.Value{p1, p2, p3}) if err != nil { fmt.Printf("error: %#v\n", err.Error()) return } fmt.Println(v.String())
Output: resolved at last!
func Await ΒΆ
Await waits for the given Promise to be resolved or rejected.
Similarly to JavaScript's await, if p isn't a Promise, it is returned as the result.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
func New ΒΆ
New creates a JavaScript Promise.
Returns the Promise, and the resolve/reject callback funcs.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise
func Race ΒΆ added in v1.1.0
Race returns the result of the first fulfilled or rejected promise in a given list of promises.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
Example ΒΆ
p1, resolve1, _ := promise.New() p2, resolve2, _ := promise.New() time.AfterFunc(200*time.Millisecond, func() { resolve1("second!") }) time.AfterFunc(100*time.Millisecond, func() { resolve2("first!") }) v, err := promise.Race([]js.Value{p1, p2}) if err != nil { fmt.Printf("error: %#v\n", err.Error()) return } fmt.Println(v.String())
Output: first!
func Reject ΒΆ added in v1.1.0
Reject creates a JavaScript Promise rejected with a given value.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject
func Resolve ΒΆ added in v1.1.0
Resolve creates a JavaScript Promise resolved with a given value.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
Types ΒΆ
type AggregateError ΒΆ added in v1.1.0
AggregateError is a JavaScript object representing an error when several errors need to be wrapped in a single error.
It is returned by promise.Any.
It can be converted back to js.Error if needed.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
func (AggregateError) Error ΒΆ added in v1.1.0
func (err AggregateError) Error() string
Error returns the string property "message" of the error.
func (AggregateError) Errors ΒΆ added in v1.1.0
func (err AggregateError) Errors() []error
Errors returns the list of errors wrapped by the error.
type Reason ΒΆ added in v1.1.0
Reason is a JavaScript value a promise was rejected with.
It implements the error interface.
It can be converted back to js.Value if needed.
type Result ΒΆ added in v1.1.0
Result is a JavaScript object that describes the outcome of a promise.
func AllSettled ΒΆ added in v1.1.0
AllSettled waits for a given list of promises to be fulfilled or rejected.
It returns a list of promise.Result which describes the outcome of each input promises.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
Example ΒΆ
results := promise.AllSettled([]js.Value{ promise.Resolve(1), promise.Reject(2), promise.Resolve(3), }) for _, r := range results { switch r.Status() { case "fulfilled": fmt.Printf("resolved: %#v\n", r.Value().Int()) case "rejected": fmt.Printf("rejected: %#v\n", r.Reason().Error()) } }
Output: resolved: 1 rejected: "2" resolved: 3
func (Result) Reason ΒΆ added in v1.1.0
Reason returns the value the promise was rejected with if status is "rejected", js.Undefined() otherwise.