promise

package module
v0.0.0-...-085af80 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 3 Imported by: 0

README

PROMISE

Go Report Card Build Status Go Reference

Install

$ go get -u github.com/chebyrash/promise

Introduction

promise allows you to write async code in sync fashion

Supports 1.18 generics and automatic panic recovery

Usage Example

package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/chebyrash/promise"
)

func main() {
	p1 := promise.New(func(resolve func(int), reject func(error)) {
		factorial := findFactorial(20)
		resolve(factorial)
	})
	p2 := promise.New(func(resolve func(string), reject func(error)) {
		ip, err := fetchIP()
		if err != nil {
			reject(err)
			return
		}
		resolve(ip)
	})

	factorial, _ := p1.Await()
	fmt.Println(factorial)

	IP, _ := p2.Await()
	fmt.Println(IP)
}

func findFactorial(n int) int {
	if n == 1 {
		return 1
	}
	return n * findFactorial(n-1)
}

func fetchIP() (string, error) {
	resp, err := http.Get("https://httpbin.org/ip")
	if err != nil {
		return "", err
	}

	type Response struct {
	    Origin string `json:"origin"`
	}
	var response Response

	err = json.NewDecoder(resp.Body).Decode(&response)
	return response.Origin, err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Promise

type Promise[T any] struct {
	// contains filtered or unexported fields
}

Promise represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

func All

func All[T any](promises ...*Promise[T]) *Promise[[]T]

All resolves when all of the input's promises have resolved. All rejects immediately upon any of the input promises rejecting. All returns nil if the input is empty.

func Any

func Any[T any](promises ...*Promise[T]) *Promise[T]

Any resolves as soon as any of the input's Promises resolve, with the value of the resolved Promise. Any rejects if all of the given Promises are rejected with a combination of all errors. Any returns nil if the input is empty.

func Catch

func Catch[T any](promise *Promise[T], rejection func(err error) error) *Promise[T]

Catch allows to chain promises. Use it to add an error handler to the rejected promise.

func New

func New[T any](executor func(resolve func(T), reject func(error))) *Promise[T]

New creates a new Promise

func Race

func Race[T any](promises ...*Promise[T]) *Promise[T]

Race resolves or rejects as soon as one of the input's Promises resolve or reject, with the value or error of that Promise. Race returns nil if the input is empty.

func Reject

func Reject[T any](err error) *Promise[T]

Reject returns a Promise that has been rejected with a given error.

func Resolve

func Resolve[T any](resolution T) *Promise[T]

Resolve returns a Promise that has been resolved with a given value.

func Then

func Then[A, B any](promise *Promise[A], resolveA func(data A) B) *Promise[B]

Then allows to chain promises. Use it to add a handler to the resolved promise.

func (*Promise[T]) Await

func (p *Promise[T]) Await() (T, error)

Await blocks until the promise is resolved or rejected.

Jump to

Keyboard shortcuts

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