GithubHelp home page GithubHelp logo

futures's Introduction

Futures

Future

A future provides idempotent access to the result of a concurrent process.

future, completeFunc := futures.NewFuture()
go func() {
    time.Sleep(1 * time.Second)
    completeFunc("future completed!", nil)
}()
<-future.Done()
result, err := future.Result()
fmt.Print(result, " ", err) // future completed! <nil>

Stream

A stream is an unbounded channel with idempotent error handling and cloning for broadcast.

stream, sendFunc := futures.NewStream()
clone := stream.Clone()

sendFunc("stream item!", nil)
streamItem, streamErr := stream.Next()
fmt.Print(streamItem, " ", streamErr) // stream item! <nil>
cloneItem, cloneErr := clone.Next()
fmt.Print(cloneItem, " ", cloneErr) // stream item! <nil>

sendFunc(nil, errors.New("stream closed!"))
streamItem2, streamErr2 := stream.Next()
fmt.Print(streamItem2, " ", streamErr2) // <nil> stream closed!
cloneItem2, cloneErr2 := clone.Next()
fmt.Print(cloneItem2, " ", cloneErr2) // <nil> stream closed!

stream.Close()
clone.Close()

Abort

Abort extends context.Context with support for custom error messages.

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
abortCtx, abortFunc := futures.WithAbort(ctx)
go func() {
    time.Sleep(1 * time.Second)
    abortFunc(errors.New("goroutine aborted!"))
}()
<-abortCtx.Done()
fmt.Print(abortCtx.Err()) // goroutine aborted!
abortCtx, abortFunc := futures.NewAbort()
ctx, cancel := context.WithCancel(abortCtx)
defer cancel()
go func() {
    time.Sleep(1 * time.Second)
    abortFunc(errors.New("goroutine aborted!"))
}()
<-ctx.Done()
fmt.Print(ctx.Err()) // goroutine aborted!

futures's People

Contributors

kevindejong avatar caioaao avatar

Watchers

 avatar

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.