Skip to content
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

Subscribe *Promise Pending Status #562

Open
gastraight opened this issue Mar 24, 2024 · 1 comment
Open

Subscribe *Promise Pending Status #562

gastraight opened this issue Mar 24, 2024 · 1 comment

Comments

@gastraight
Copy link

gastraight commented Mar 24, 2024

Hi.
Please while having a *Promise exposed out of a js runtime environment into golang one in case of PromiseStatePending what would be the best option to wait/subscribe for event once it is naturally resolved/rejected within the initial runtime? (Except of just a periodical state check)

E.g.

        value, err := vm.RunString(`
			new Promise(resolve  => setTimeout(resolve, 1000))
	`)
	if err != nil {
		panic(err)
	}
	if p, ok := value.Export().(*goja.Promise); ok {
		switch p.State() {
		case goja.PromiseStateRejected:
			panic(p.Result().String())
		case goja.PromiseStateFulfilled:
			result = p.Result().Export()
		default:
			// how to get notified once it is resolved/rejected 
		}
	}

For the very same reason please clarify from the docs of NewPromise

The returned values are not goroutine-safe and must not be called in parallel with VM running. In order to make use of this method you need an event loop

Do I see it right that runtime.RunString and all the similar handles its own event loop under the hood. And once runtime.RunString execution is ended (returned a value) whether underling event loop still keep on further processing?

Thnx

@gastraight
Copy link
Author

P.S. I mean whether it is possible to subscribe to a state change purely on a golang side without extra hack wrapping at js as

new Promise(resolve  => setTimeout(resolve, 1000)).then(goResolver, goRejector)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant