-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a promises
package with a New
helper function
#3176
Conversation
5273a18
to
8b8df8b
Compare
Codecov Report
@@ Coverage Diff @@
## master #3176 +/- ##
==========================================
- Coverage 72.82% 72.80% -0.02%
==========================================
Files 255 253 -2
Lines 19611 19663 +52
==========================================
+ Hits 14281 14315 +34
- Misses 4433 4446 +13
- Partials 897 902 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
|
MakeHandledPromise
helper function to modules
packagepromises
package with a MakeHandledPromise
helper function
This helper function makes it convenient to create a promise in the context of k6 module/extension Go code.
8b8df8b
to
606d2ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM in general.
I have some worries about the documentation. I also find the name ... strange. And I do know big parts of this come from my original code, years ago.
But promises.NewHandledPromise(vu)
doesn't really tell you what "handled" means.
I don't have a particular proposition on that part though. My only thought is promises.NewEasyPromise
as I guess that is the main part people care about - it is easy(ier) to use.
js/promises/promises.go
Outdated
// Calling the function will create a goja promise and return its `resolve` and `reject` callbacks, wrapped | ||
// in such a way that it will block the k6 JS runtime's event loop from exiting before they are | ||
// called, even if the promise isn't revoled by the time the current script ends executing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current documentation:
- does not explain why you would use that instead of
goja.NewRuntime()
- Explains things that arguably are better left explain in the eventloop package - and they are.
I would propose that we do actually document why you will want to use this and simple say it uses the underlying eventloop mechanism.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point 👍🏻 Will adapt it indeed 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's an attempt at improving the documentation accordingly:
// MakeHandledPromise facilitates the creation of promises within k6 modules.
//
// Whenever a k6 module function or methods returns a promise, it very likely
// should be created using this function as it ensures the promise is correctly
// managed by k6's event loop.
//
// The function returns a promise, a resolve function and a reject function. The
// promise is the one that should be returned by the function, and the `resolve`
// and `reject` functions should be used to resolve or reject the promise.
//
// Usage example:
//
// func myAsynchronousFunction(vu modules.VU) *goja.Promise {
// promise, resolve, reject := CreateAsyncFunctionPromise(vu)
// go func() {
// value, err := someAsynchronousOperation()
// if err != nil {
// reject(err)
// return
// }
//
// resolve(value)
// }()
// return promise
// }
//
// In the above example, `someAsynchronousOperation` represents an operation that might take a while to finish,
// and k6 script execution shouldn't be halted during its execution. This function ensures such an operation
// is correctly managed within k6's event loop.
WDYT? Suggestions to improve?
Co-authored-by: Mihail Stoykov <312246+mstoykov@users.noreply.github.com>
Co-authored-by: Ivan <2103732+codebien@users.noreply.github.com>
Co-authored-by: Ivan <2103732+codebien@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Ivan <2103732+codebien@users.noreply.github.com>
promises
package with a MakeHandledPromise
helper functionpromises
package with a New
helper function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oleiade a simple test should be added, ofc you can do it in a different low-priority PR.
What?
This Pull Request introduces a
promises
package (underjs
) with a singleNew
helper function facilitating the creation of a promise and obtaining itsresolve
andreject
callback from Go code.Why?
We noticed in the context of writing extensions and experimental modules that we would consistently rewrite the same function again and again. Thus, this specific PR aims at declaring this function in a reusable place within the k6 project.
None of this is definitive, and really is a proposal, if you have improvement ideas, shoot 🚀
Checklist
make ci-like-lint
) and all checks pass.make tests
) and all tests pass.Related PR(s)/Issue(s)
ref #3165