Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 19c5b33

Browse files
committed
Refactor promise helpers to fix Interrupt with async code
This changes the promise helpers so after grafana/k6#4017 async code and Interrupt will work. AbortingPromises seems to have not been used at all and just adds complexity. And `promises.New()` already does the remaining things needed to happen, so this can be merged even without grafana/k6#4017.
1 parent e76269a commit 19c5b33

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

k6ext/promise.go

+8-32
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@ import (
44
"context"
55

66
"github.com/grafana/sobek"
7-
)
8-
9-
// eventLoopDirective determines whether the event
10-
// loop should be aborted if the promise is rejected.
11-
type eventLoopDirective int
12-
13-
const (
14-
continueEventLoop eventLoopDirective = iota + 1
15-
abortEventLoop
7+
"go.k6.io/k6/js/promises"
168
)
179

1810
// PromisifiedFunc is a type of the function to run as a promise.
@@ -23,33 +15,17 @@ type PromisifiedFunc func() (result any, reason error)
2315
// first result value fn returns.
2416
// - Otherwise, rejects the promise with the error fn returns.
2517
func Promise(ctx context.Context, fn PromisifiedFunc) *sobek.Promise {
26-
return promise(ctx, fn, continueEventLoop)
27-
}
28-
29-
// AbortingPromise is like Promise, but it aborts the event loop if an error occurs.
30-
func AbortingPromise(ctx context.Context, fn PromisifiedFunc) *sobek.Promise {
31-
return promise(ctx, fn, abortEventLoop)
18+
return promise(ctx, fn)
3219
}
3320

34-
func promise(ctx context.Context, fn PromisifiedFunc, d eventLoopDirective) *sobek.Promise {
35-
var (
36-
vu = GetVU(ctx)
37-
cb = vu.RegisterCallback()
38-
p, resolve, reject = vu.Runtime().NewPromise()
39-
)
21+
func promise(ctx context.Context, fn PromisifiedFunc) *sobek.Promise {
22+
p, resolve, reject := promises.New(GetVU(ctx))
4023
go func() {
4124
v, err := fn()
42-
cb(func() error {
43-
if err != nil {
44-
reject(err)
45-
} else {
46-
resolve(v)
47-
}
48-
if d == continueEventLoop {
49-
err = nil
50-
}
51-
return err
52-
})
25+
if err != nil {
26+
reject(err)
27+
}
28+
resolve(v)
5329
}()
5430

5531
return p

0 commit comments

Comments
 (0)