Git Product home page Git Product logo

asyncgo's Introduction

asyncgo

GitHub license

Boilerplate implementing async pattern

Basic

You can create asynchronous tasks in the following form:

// Executes asynchronous tasks (goroutines) immediately.
// The returned task object is a channel wrapper that holds the execution context of the goroutine.
task := async.RunTask(func(args ...any) int {
    ...
    return 10;
})

taskResult := task.Await() // Waits until the task completes and returns a value.
fmt.Println(taskResult) // 10

Parameters can also be passed to Tasks. This allows you to clearly distinguish between task input and output.

// Executes asynchronous tasks (goroutines) immediately.
// The returned task object is a channel wrapper that holds the execution context of the goroutine.
task := async.RunTask(func(args ...any) int {
    lhs := args[0].(int)
    rhs := args[1].(int)
    return lhs + rhs;
}, 10, 20)

taskResult := task.Await()
fmt.Println(taskResult) // 30

If you absolutely need to recover when a panic occurs within a task, you can use RunPanicableTask. In this case, you must use the Result type to handle panic propagation.

task := async.RunPanicableTask(func(args ...any) async.Result[int] {
    ... // If a panic occurs, async.Err is returned.
    return async.Ok(10);
})

taskResult := task.Await()
fmt.Println(taskResult)

If you want to wait for multiple tasks simultaneously, you can use the JoinAll function.

task1 := ...
task2 := ...

// Wait for both task1 and task2 to finish.
results := async.JoinAll(task1, task2).Await()

for _, result := results {
    ...
}

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.