-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromise.go
More file actions
179 lines (154 loc) · 5.32 KB
/
promise.go
File metadata and controls
179 lines (154 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//go:build js
package goji
import (
"context"
"syscall/js"
)
func init() {
Promise = promiseJS(js.Global().Get("Promise"))
}
type promiseJS js.Value
// Promise is a wrapper for the Promise global object.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
var Promise promiseJS
// New wraps the Promise constructor.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise
func (p promiseJS) New(executor js.Func) PromiseValue {
res := js.Value(p).New(executor)
return PromiseValue(res)
}
// All wraps the Promise all static method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
func (p promiseJS) All(iterable js.Value) PromiseValue {
res := js.Value(p).Call("all", iterable)
return PromiseValue(res)
}
// AllSettled wraps the Promise allSettled static method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
func (p promiseJS) AllSettled(iterable js.Value) PromiseValue {
res := js.Value(p).Call("allSettled", iterable)
return PromiseValue(res)
}
// Any wraps the Promise any static method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any
func (p promiseJS) Any(iterable js.Value) PromiseValue {
res := js.Value(p).Call("any", iterable)
return PromiseValue(res)
}
// Race wraps the Promise race static method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race
func (p promiseJS) Race(iterable js.Value) PromiseValue {
res := js.Value(p).Call("race", iterable)
return PromiseValue(res)
}
// Reject wraps the Promise reject static method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject
func (p promiseJS) Reject(value js.Value) PromiseValue {
res := js.Value(p).Call("reject", value)
return PromiseValue(res)
}
// Resolve wraps the Promise resolve static method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
func (p promiseJS) Resolve(value js.Value) PromiseValue {
res := js.Value(p).Call("resolve", value)
return PromiseValue(res)
}
// PromiseValue is an instance of Promise.
type PromiseValue js.Value
// Then wraps the Promise then prototype method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
func (v PromiseValue) Then(onFulfilled js.Func) PromiseValue {
res := js.Value(v).Call("then", onFulfilled)
return PromiseValue(res)
}
// Catch wraps the Promise catch prototype method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
func (v PromiseValue) Catch(onRejected js.Func) PromiseValue {
res := js.Value(v).Call("catch", onRejected)
return PromiseValue(res)
}
// Finally wraps the Promise finally prototype method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally
func (v PromiseValue) Finally(onFinally js.Value) PromiseValue {
res := js.Value(v).Call("catch", onFinally)
return PromiseValue(res)
}
// PromiseOf is a helper function that wraps the given func in a promise.
func PromiseOf(fn func(resolve, reject func(value js.Value))) PromiseValue {
var executor js.Func
executor = js.FuncOf(func(this js.Value, args []js.Value) any {
executor.Release()
resolve := func(value js.Value) {
args[0].Invoke(value)
}
reject := func(value js.Value) {
args[1].Invoke(value)
}
// the function must run in a new go routine
// to avoid blocking the render thread in JS
go fn(resolve, reject)
return js.Undefined()
})
return Promise.New(executor)
}
// awaitResult contains the promise results.
type awaitResult struct {
val []js.Value
err error
}
// AwaitContext is a helper function that waits for a promise to resolve or reject
// and returns the results and an error value.
//
// This helper function supports context cancellation.
func AwaitContext(ctx context.Context, promise PromiseValue) ([]js.Value, error) {
res := make(chan awaitResult)
defer close(res)
onFulfilled := js.FuncOf(func(this js.Value, args []js.Value) any {
res <- awaitResult{val: args}
return js.Undefined()
})
defer onFulfilled.Release()
onRejected := js.FuncOf(func(this js.Value, args []js.Value) any {
res <- awaitResult{err: ErrorValue(args[0])}
return js.Undefined()
})
defer onRejected.Release()
promise.Then(onFulfilled).Catch(onRejected)
select {
case <-ctx.Done():
return nil, ctx.Err()
case out := <-res:
return out.val, out.err
}
}
// Await is a helper function that waits for a promise to resolve or reject
// and returns the results and an error value.
func Await(promise PromiseValue) ([]js.Value, error) {
return AwaitContext(context.Background(), promise)
}
// Async is a helper function that wraps the given func in a promise that
// resolves when no error is returned or rejects when an error is returned.
func Async(fn func(this js.Value, args []js.Value) (js.Value, error)) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) any {
prom := PromiseOf(func(resolve, reject func(value js.Value)) {
res, err := fn(this, args)
if err != nil {
reject(js.Value(Error.New(err.Error())))
} else {
resolve(res)
}
})
return js.Value(prom)
})
}