代码拉取完成,页面将自动刷新
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Ignore leaks within Go standard libraries http/https support code.
// The getaddrinfo detected leaks can be avoided using GODEBUG=netdns=go but
// currently there are more for loading system root certificates on macOS.
//go:build !leakcheck || !darwin
// +build !leakcheck !darwin
package v8go_test
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
v8 "gitee.com/hasika/v8go"
)
func ExampleFunctionTemplate_fetch() {
iso := v8.NewIsolate()
defer iso.Dispose()
global := v8.NewObjectTemplate(iso)
fetchfn := v8.NewFunctionTemplate(iso, func(info *v8.FunctionCallbackInfo) *v8.Value {
args := info.Args()
url := args[0].String()
resolver, _ := v8.NewPromiseResolver(info.Context())
go func() {
res, _ := http.Get(url)
body, _ := ioutil.ReadAll(res.Body)
val, _ := v8.NewValue(iso, string(body))
resolver.Resolve(val)
}()
return resolver.GetPromise().Value
})
global.Set("fetch", fetchfn, v8.ReadOnly)
ctx := v8.NewContextWithOptions(iso, global)
defer ctx.Close()
val, _ := ctx.RunScript("fetch('https://rogchap.com/v8go')", "")
prom, _ := val.AsPromise()
// wait for the promise to resolve
for prom.State() == v8.Pending {
continue
}
fmt.Printf("%s\n", strings.Split(prom.Result().String(), "\n")[0])
// Output:
// <!DOCTYPE html>
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。