Skip to content

martinohmann/promise

Repository files navigation

Promise

Build Status codecov GoDoc Go Report Card GitHub

Initially this started out as an experiment to better understand the inner workings of JavaScript promises and how this can be implemented from scratch in go.

The result is a promise implementation which follows the Promises/A+ Standard and comes with the following additional features:

  • Waiting for promise resolution.
  • Panics during promise resolution cause the promise to be rejected with the panic message.
  • Nested promise resolution.
  • Race, All, Any and AllSettled extensions to handle the parallel resolution of multiple promises.
  • Promise pooling. See the promise.Pool documentation and the pooling example.
  • Promise instrumentation for tracing, logging and debugging. See the instrumented package documentation for more information.

Head over to the promise godoc for the API documentation.

Installation

go get -u github.com/martinohmann/promise

Usage

Check out the examples in the _examples/ directory to see promises in action.

Simple example

package main

import (
	"errors"
	"fmt"
	"math/rand"
	"time"

	"github.com/martinohmann/promise"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func main() {
	p := promise.New(func(resolve promise.ResolveFunc, reject promise.RejectFunc) {
		// simulate some computation
		sleepDuration := time.Duration(rand.Int63n(2000)) * time.Millisecond
		time.Sleep(sleepDuration)

		fmt.Printf("computation took %s\n", sleepDuration)

		// inject some random errors
		if rand.Int63n(2) == 0 {
			reject(errors.New("computation failed"))
			return
		}

		// simulate computation result
		resolve(rand.Int63())
	}).Then(func(val promise.Value) promise.Value {
		fmt.Printf("computation result: %d\n", val.(int64))
		return val
	}).Catch(func(err error) promise.Value {
		fmt.Printf("error during computation: %v\n", err)
		return err
	})

	// Wait for the promise resolution to be complete, that is: either fulfillment or rejection.
	val, err := p.Await()
	if err != nil {
		fmt.Printf("Promise rejected with error: %v\n", err)
	} else {
		fmt.Printf("Promise fulfilled with value: %d\n", val.(int64))
	}
}

License

The source code of promise is released under the MIT License. See the bundled LICENSE file for details.

Releases

No releases published

Packages

No packages published