fetch

package
v0.0.0-...-4140cf8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2022 License: MIT Imports: 11 Imported by: 0

README

fetch godoc

a window.fetch JavaScript polyfill

Usage

Install via go get https://github.com/olebedev/gojax/fetch.

package main

import (
	"fmt"

	goproxy "gopkg.in/elazarl/goproxy.v1"

	"github.com/dop251/goja"
	"github.com/dop251/goja_nodejs/eventloop"
	"github.com/olebedev/gojax/fetch"
)

func main() {
	loop := eventloop.NewEventLoop()
	loop.Start()
	defer loop.Stop()

	fetch.Enable(loop, goproxy.NewProxyHttpServer())

	wait := make(chan string, 1)
	loop.RunOnLoop(func(vm *goja.Runtime) {
		vm.Set("callback", func(call goja.FunctionCall) goja.Value {
			wait <- call.Argument(0).ToString().String()
			return nil
		})

		vm.RunString(`
			fetch('https://ya.ru').then(function(resp){
				return resp.text();
			}).then(function(resp){
				callback(resp.slice(0, 15));
			});
		`)
	})
	fmt.Println(<-wait)
}

This program will prints <!DOCTYPE html> into stdout. See fetch_test.go for more examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enable

func Enable(loop *eventloop.EventLoop, proxy http.Handler) error

Enable enables fetch for the instance. Loop instance is required instead of flat goja's. B/c fetch polyfill uses timeouts for promises.

The second parameter could be any http handler. Even you local instance, to handle http requests locally programmatically.

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL