Fetch polyfill for go-duktape.
First of all install the package go get gopkg.in/olebedev/go-duktape-fetch.v3
.
package main
import (
"fmt"
"gopkg.in/olebedev/go-duktape.v3"
"gopkg.in/olebedev/go-duktape-fetch.v3"
)
func main() {
// create an ecmascript context
ctx := duktape.New()
// push fetch into the global scope
fetch.Define(ctx)
ch := make(chan string)
ctx.PushGlobalGoFunction("cbk", func(c *duktape.Context) int {
ch <- c.SafeToString(-1)
return 0
})
// make a request
ctx.PevalString(`
fetch('http://ya.ru').then(function(resp) {
return resp.text();
}).then(function(body) {
// release channel
cbk(body.slice(0, 15));
});
`)
// print head line of the response body
fmt.Println(<-ch)
}
This program will produce <!DOCTYPE html>
into stdout.