Skip to content
Snippets Groups Projects
Commit 0f4b067e authored by Jono Wenger's avatar Jono Wenger
Browse files

Add Await, which allows Go to wait for Javascript promises

parent f9091a63
No related branches found
No related tags found
1 merge request!60Revert "Fail a test to be sure it works"
......@@ -71,3 +71,32 @@ func CreatePromise(f PromiseFn) interface{} {
// Create and return the Promise object
return Promise.New(handler)
}
// Await waits on a Javascript value. It returns the results of the then and
// catch functions once it resolves.
func Await(awaitable js.Value) ([]js.Value, []js.Value) {
then := make(chan []js.Value)
defer close(then)
thenFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
then <- args
return nil
})
defer thenFunc.Release()
catch := make(chan []js.Value)
defer close(catch)
catchFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
catch <- args
return nil
})
defer catchFunc.Release()
awaitable.Call("then", thenFunc).Call("catch", catchFunc)
select {
case result := <-then:
return result, nil
case err := <-catch:
return nil, err
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment