Giter Club home page Giter Club logo

Comments (4)

miaozilong avatar miaozilong commented on May 26, 2024 1

i solved the problem !

package main

import (
	"errors"
	"fmt"
	"github.com/chebyrash/promise"
	"io/ioutil"
	"net/http"
	"time"
)

func main() {
	var requestPromise = promise.New(func(resolve func(interface{}), reject func(error)) {
		go func() {
			time.Sleep(2 * time.Second)
			reject(errors.New("time out error"))
		}()
		resp, err := http.Get("https://httpbin.org/ip")
		defer resp.Body.Close()
		if err != nil {
			reject(err)
			return
		}
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			reject(err)
			return
		}
		resolve(body)
	})

	// Parse JSON body in async manner
	parsed, err := requestPromise.Await()

	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
		return
	}
	ip := fmt.Sprintf("%s", parsed)

	fmt.Println(ip)
}

from promise.

chebyrash avatar chebyrash commented on May 26, 2024

Hi,

Have a look at https://golang.org/pkg/net/http/#Client
The easiest way is to use is the Timeout field of http.Client

c := &http.Client{
    Timeout: 15 * time.Second,
}
resp, err := c.Get("https://httpbin.org/ip")

Or you can use

go func() {
    time.Sleep(2 * time.Second)
    reject(errors.New("time out error"))
}()

from promise.

miaozilong avatar miaozilong commented on May 26, 2024

i want to use
promise.Wait(2*time.Second)
how to solve it it's more useful

from promise.

miaozilong avatar miaozilong commented on May 26, 2024
parsed, err := requestPromise.Await( 2 * time.Second )

how to use like this?

from promise.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.