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

how to get a resolve value in a promise? #508

Open
czz362100 opened this issue May 25, 2023 · 3 comments
Open

how to get a resolve value in a promise? #508

czz362100 opened this issue May 25, 2023 · 3 comments

Comments

@czz362100
Copy link

czz362100 commented May 25, 2023

hey team, i have some confusions in these code

vm := goja.New()
	promise, err := vm.RunString(`
			async function sum(a, b) {
    			return a+b;
			}
	`)
	if err != nil {
		panic(err)
	}
	add, ok := goja.AssertFunction(vm.Get("sum"))
	if !ok {
		panic("not a function")
	}

	value, _ := add(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))
	return value

in this example, the sum Function return a [object, Promise], but i want to get a Resolved value, please tell me how do i?

@czz362100 czz362100 changed the title how to get a fullvalue in a promise? how to get a resolve value in a promise? May 25, 2023
@shiroyk
Copy link
Contributor

shiroyk commented May 29, 2023

Export to promise

func main() {
	vm := goja.New()
	_, err := vm.RunString(`
			async function sum(a, b) {
    			return a+b;
			}
	`)
	if err != nil {
		panic(err)
	}
	add, ok := goja.AssertFunction(vm.Get("sum"))
	if !ok {
		panic("not a function")
	}

	value, _ := add(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))

	var result any
	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:
			panic("unexpected promise state pending")
		}
	}

	fmt.Println(result)
}

@cjf621
Copy link

cjf621 commented Jun 5, 2023

Export to promise

func main() {
	vm := goja.New()
	_, err := vm.RunString(`
			async function sum(a, b) {
    			return a+b;
			}
	`)
	if err != nil {
		panic(err)
	}
	add, ok := goja.AssertFunction(vm.Get("sum"))
	if !ok {
		panic("not a function")
	}

	value, _ := add(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))

	var result any
	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:
			panic("unexpected promise state pending")
		}
	}

	fmt.Println(result)
}

I would like to ask a question,the promise is async,so in goja in golang it is sync?If the execution is not completed when the results are obtained,it is true?

@shiroyk
Copy link
Contributor

shiroyk commented Jun 7, 2023

Export to promise

func main() {
	vm := goja.New()
	_, err := vm.RunString(`
			async function sum(a, b) {
    			return a+b;
			}
	`)
	if err != nil {
		panic(err)
	}
	add, ok := goja.AssertFunction(vm.Get("sum"))
	if !ok {
		panic("not a function")
	}

	value, _ := add(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))

	var result any
	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:
			panic("unexpected promise state pending")
		}
	}

	fmt.Println(result)
}

I would like to ask a question,the promise is async,so in goja in golang it is sync?If the execution is not completed when the results are obtained,it is true?

It's async, you can also see

goja/builtin_promise.go

Lines 592 to 611 in 28ee0ee

// NewPromise creates and returns a Promise and resolving functions for it.
//
// WARNING: 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 such as the one in goja_nodejs (https://github.com/dop251/goja_nodejs)
// where it can be used like this:
//
// loop := NewEventLoop()
// loop.Start()
// defer loop.Stop()
// loop.RunOnLoop(func(vm *goja.Runtime) {
// p, resolve, _ := vm.NewPromise()
// vm.Set("p", p)
// go func() {
// time.Sleep(500 * time.Millisecond) // or perform any other blocking operation
// loop.RunOnLoop(func(*goja.Runtime) { // resolve() must be called on the loop, cannot call it here
// resolve(result)
// })
// }()
// }
func (r *Runtime) NewPromise() (promise *Promise, resolve func(result interface{}), reject func(reason interface{})) {

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

3 participants