Help us shape the future of Go! Take the 2026 Go Developer Survey

promise

package module
v0.0.0-...-6321ee9 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 0 Imported by: 0

README

Go Promise 🐰

Manage async flow as a javascript person

How to use

package main

import (
	"errors"
	"fmt"
	"net/http"

	"github.com/igorhalfeld/go-promise"
)

func main() {
	p := promise.New(func(resolve func(interface{}), reject func(error)) {
		resp, err := http.Get("http://gobyexample.com")
		if err != nil {
			panic(err)
		}
		defer resp.Body.Close()

		if resp.StatusCode == 200 {
			resolve(resp.Status)
		} else {
			reject(errors.New("ERRR"))
		}
	})

	p.Then(func(value interface{}) {
		fmt.Println("Success", value)
	}).Catch(func(err error) {
		fmt.Println("Error", err)
	})

	p.Wait()
}

Contributors

brenin João Pedro
Breno Andrade João Pedro

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Promise

type Promise struct {
	// contains filtered or unexported fields
}

Promise represents a promise struct

func New

func New(fn func(func(interface{}), func(error))) *Promise

New represents a new instance of Promise struct

func (*Promise) Catch

func (p *Promise) Catch(failure func(error)) *Promise

Catch represents a next chain of failure flow

func (*Promise) Then

func (p *Promise) Then(success func(interface{})) *Promise

Then represents a next chain of success flow

func (*Promise) Wait

func (p *Promise) Wait()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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