Giter Club home page Giter Club logo

Comments (3)

shiroyk avatar shiroyk commented on August 16, 2024

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

from goja.

cjf621 avatar cjf621 commented on August 16, 2024

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?

from goja.

shiroyk avatar shiroyk commented on August 16, 2024

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

from goja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.