goja icon indicating copy to clipboard operation
goja copied to clipboard

how to get a resolve value in a promise?

Open czz362100 opened this issue 2 years ago • 3 comments

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 avatar May 25 '23 10:05 czz362100

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)
}

shiroyk avatar May 29 '23 08:05 shiroyk

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?

72wo avatar Jun 05 '23 14:06 72wo

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 https://github.com/dop251/goja/blob/28ee0ee714f3e8218c14e2cd2d7a67ec94848c21/builtin_promise.go#L592-L611

shiroyk avatar Jun 07 '23 01:06 shiroyk