Giter Site home page Giter Site logo

future's Introduction

future

An implementation of futures/promises for go1.18+ type parameters (generics).

Why would I want this?

Go's programming model of implicitly yielding to other goroutines leads to easy to read code, without extra syntax. Other languages might require additional keywords, callback functions, etc.

But async/await with Promises in JavaScript lets you choose if you want to have the current execution context block and yield to other contexts while waiting for results, or do other work in the meantime. Doing this in Go requires running new goroutines and synchronizing with channels or mutexes. This is powerful and flexible, but sometimes it's too much boilerplate.

Where async/await style languages let you easily opt-in to waiting, what if you could easily opt-out of waiting in Go?

Example

package main

import (
	"context"
	"fmt"
	"time"

    "github.com/jbowes/future"
)

func SomeAsyncThing(ctx context.Context) (int, error) {
	fmt.Println("doing some async thing")
	time.Sleep(2 * time.Second)
	return 2, nil
}

func main() {
	ctx := context.Background()

	// Create a new future to do SomeAsyncThing in the background
	fut := future.New(func() (int, error) { return SomeAsyncThing(ctx) })

	time.Sleep(1 * time.Second)
	fmt.Println("doing the main thing")

	// Wait for the results, if they're not done yet.
	x, err := fut.Await()
	fmt.Println("got result", x, err)
}

Limitations

An implementation for each supported argument count

There is no planned support for variadic type paramters. To support wrapping funcs with 1, 2, 3, and 4 arguments, we need 4 different implementations. For callers, this means using New or New3 etc. As Await is a method on the various Futures, its name doesn't have to change.

Awkward syntax for New

For New to work and be type safe, you must pass a function to it, with a known set of inputs (that is, zero inputs):

fut := future.New(func() (int, error) { return SomeAsyncThing(ctx, arg1, arg2) })

A nicer syntax would be one supported by variadic type parameters:

fut := future.New(SomeAsyncThing, ctx, arg1, arg2)

Supporting this with multiple implementations would require (# of inputs) * (# of outputs) implementations. For end users, calling future.New3_4 seems too unwieldy.

What about reflection?

There are a handful of reflection-based future/promise Go libraries already available. For convenient comparison, one is included here under refluture. Note that other libraries include features like promise chaining and racing, and tend to expose APIs similar to JavaScript's promises (eg resolve and reject). I couldn't be bothered to add those and don't think they fit well in Go code, where you can fall back to using goroutines, channels, select, etc.

Links

future's People

Contributors

jbowes avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

joker666

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.