rogchap / v8go

Execute JavaScript from Go

Home Page:https://rogchap.com/v8go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question]How to define async function in Go?

Laisky opened this issue · comments

Is it possible to define an async function in Go that return Promise in JS ?

PromiseResolver can be used to create and resolve a promise in Go.

PromiseResolver can be used to create and resolve a promise in Go.

  • NewPromiseResolver(ctx) need ctx, and ctx created after v8.NewContext(iso, global), so ctx need global
  • NewPromiseResolver(ctx) called before global.Set(FuncName, GoFunc), so global need ctx

that is a dilemma, I could not figure out how to solve it.

Could you kindly give an example?

The documentation has an example that uses PromiseResolver: https://pkg.go.dev/rogchap.com/v8go#example-FunctionTemplate-Fetch

  • NewPromiseResolver(ctx) called before global.Set(FuncName, GoFunc)

I'm not sure why that is the case. You don't need the promise resolver to define the async function, you just need it to create the promise to return from an async function such that it can be resolved from Go code. As such, you would typically use NewPromiseResolver when that function that is exposed to JS is called, so that a different promise is returned each time.

Thanks so much! @dylanahsmith

info.Context() solve my problem.