Comments (3)
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.
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.
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
Lines 592 to 611 in 28ee0ee
from goja.
Related Issues (20)
- ReferenceError: window is not defined at fn HOT 2
- sourcemap: runtime error: index out of range [-1] HOT 2
- [Bug] Big number representation wrong HOT 2
- Function with block-scoped assignment inside finally returns wrong value HOT 1
- proposal: pluggable panic handler HOT 5
- Symbol.asyncIterator support HOT 2
- Support for bigint? HOT 1
- AST Walk / Visitor pattern HOT 1
- Does goja support WebAssembly? or somehow run bytecode on goja? HOT 1
- Is the Web Platform in scope? HOT 1
- How to define async function in Goja HOT 4
- Subscribe *Promise Pending Status HOT 2
- How to Throw an Error From the Golang Function
- What Is Types Marshaling Among JS and GOLANG Spaces.
- Support for setting a Promise as handled to true HOT 3
- how to impove performance for a amount of constant literal in a javascript file HOT 4
- vm reuse problem HOT 2
- Request for Assistance: Obtaining Abstract Syntax Tree (AST) with Goja HOT 1
- How to limit the number of for loops HOT 1
- Fibers/Coroutines HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goja.