Coder Social home page Coder Social logo

abdullahpazarbasi / promise Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 42 KB

Go Promise

License: MIT License

Go 100.00%
future future-api go go-lib go-library go-package go-promise golang golang-library golang-package promise promise-api go-future

promise's Introduction

Go Promise

Check Status Test Status Go Report Card Go Reference GitHub release (latest by date including pre-releases) GitHub

Promise / Future library for Go

Aim

This library provides a promise interface similar to JavaScript 's

Features

  • Generics (Type Parameters) support
  • Interface similar to JS 's
  • Cancellable promises
  • Time-out support
  • Context support
  • Promises which can be committed separately
  • Promise maps
  • Race functionality
  • Automatic panic recovery
  • Fluent interface / Chain API
  • No external dependency

To-Do

no to-do

Installation

go get github.com/abdullahpazarbasi/promise/v3

Usage

Basic Usage

package foo

import (
	"context"
	"fmt"
	"github.com/abdullahpazarbasi/promise/v3"
	"time"
)

func myParentRoutine() error {
	f1 := promise.New(func(ctx context.Context) (string, error) {
		return "OK", nil
	})
	f2 := promise.New(func(ctx context.Context) (bool, error) {
		return true, nil
	})
	fmt.Println("Parallel paths are starting :")
	p1 := f1.TimeOutLimit(500 * time.Millisecond).Commit()
	p2 := f2.TimeOutLimit(400 * time.Millisecond).Commit()

	fmt.Println("Doing something on primary parallel path ...")

	fmt.Println("Waiting for one of committed promises")
	concurrentOutput1, err1 := p1.Await()
	if err1 != nil {
		return err1 // output of async function 2 ignored
	}
	fmt.Println("Waiting for the other committed promise, may be the task is already done a long time ago")
	concurrentOutput2, err2 := p2.Await()
	if err2 != nil {
		panic(err2)
	}
	fmt.Printf("Output of async function 1: %v\n", concurrentOutput1)
	fmt.Printf("Output of async function 2: %v\n", concurrentOutput2)

	return nil
}

Other Examples

examples

Contributors

List

License

This library licensed under MIT License.

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.