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

Helpers to streamline working with event loop #463

Closed
Tracked by #428
inancgumus opened this issue Jul 19, 2022 · 0 comments · Fixed by #464
Closed
Tracked by #428

Helpers to streamline working with event loop #463

inancgumus opened this issue Jul 19, 2022 · 0 comments · Fixed by #464
Assignees
Labels
async supports async (promises) enhancement New feature or request refactor tests
Milestone

Comments

@inancgumus
Copy link
Member

inancgumus commented Jul 19, 2022

Introducing some helpers can make adding Async API supporting code and tests easier. Another reason for doing this change is the k6 promise API is still experimental, and its API can change (see this).

k6ext.Promise

This helper makes it easier to switch our sync API to async.

Before:

rt := b.vu.Runtime()
cb := b.vu.RegisterCallback()
p, resolve, reject := rt.NewPromise()

go func() {
	select {
	case <-b.browserProc.lostConnection:
		cb(func() error {
			resolve(true)
			return nil
		})
	case <-b.ctx.Done():
		cb(func() error {
			reject(fmt.Errorf("browser.on promise rejected: %w", b.ctx.Err()))
			return nil
		})
	}
}()

return p

After:

return k6ext.Promise(b.ctx, func() (interface{}, error) {
	select {
	case <-b.browserProc.lostConnection:
		// resolves the promise with the value true
		return true, nil
	case <-b.ctx.Done():
		// rejects the promise with the error with the given error
		return nil, fmt.Errorf("browser.on promise rejected: %w", b.ctx.Err())
	}
})

k6ext.AbortingPromise

There can also be an AbortingPromise that will allow aborting the event loop when an error occurs. It's not clear whether we're ever going to need this, then again, it doesn't hurt to support it.

@inancgumus inancgumus self-assigned this Jul 19, 2022
@inancgumus inancgumus added this to the v0.5.0 milestone Jul 19, 2022
@inancgumus inancgumus added refactor async supports async (promises) labels Jul 19, 2022
@inancgumus inancgumus changed the title Refactor for Async API Add helpers for the Async APIs support Jul 19, 2022
@inancgumus inancgumus added enhancement New feature or request tests labels Jul 19, 2022
@inancgumus inancgumus changed the title Add helpers for the Async APIs support Helpers to streamline working with event loop May 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
async supports async (promises) enhancement New feature or request refactor tests
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant