Gopherjs: Callback Question

Created on 13 Oct 2016  路  3Comments  路  Source: gopherjs/gopherjs

This is probably really dense on my part, but how do I handle callbacks in gopherjs?

EG:

window.doSomething(constraints).then(handleSuccess).catch(handleError);

question

All 3 comments

I found examples of deferred calls in the Jquery wrapper. I think I am starting to understand this a bit more.

https://github.com/gopherjs/jquery/blob/master/jquery.go#L828

window.doSomething(constraints).then(handleSuccess).catch(handleError);

Using js package, that would be:

handleSuccess := func(result *js.Object) { ... }
handleError := func(...) { ... }

js.Global.Call("doSomething", constraints).Call("then", handleSuccess).Call("catch", handleError)

You can also see an example here.

Glad to hear it's starting to make sense.

FWIW, if you can refactor your code to write on a channel, that's listened to by a goroutine, that might be better. No point in just rewriting Javascript in Go, _if_ you can avoid it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

j7b picture j7b  路  4Comments

hajimehoshi picture hajimehoshi  路  6Comments

hackwaly picture hackwaly  路  4Comments

nytehauq picture nytehauq  路  7Comments

hajimehoshi picture hajimehoshi  路  6Comments