Code Monkey home page Code Monkey logo

promise's Introduction

Go Promise package

A simple library that allows you to create promises with go.

Installation

go get github.com/4strodev/promise

Examples

Creating a promise

package main

import (
	"context"
	"fmt"
	"github.com/4strodev/promise/pkg"
	"time"
)

func main() {
	p := promise.New(func(resolve func(int), reject func(error)) {
		time.Sleep(1 * time.Second)
		resolve(1)
	})

	result, err := p.Await(context.Background())
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
	// Output: 1
}

Working with multiple promises

package main

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

	"github.com/4strodev/promise/pkg"
)

type Job struct {
	Value1 int
	Value2 int
}

func main() {
	jobs := []Job{}
	promises := []*promise.Promise[int]{}
	for i := 0; i < 10; i++ {
		job := Job{
			Value1: rand.Intn(10),
			Value2: rand.Intn(10),
		}
		jobs = append(jobs, job)
	}

	for _, _job := range jobs {
		// For new go developers loop variables are loop scoped
		// that means that the variable _job is declared only once
		// and for each iteration the value is overrited
		// that caused a lot of bugs and will be changed in go 1.22
		// for more information see https://go.dev/blog/loopvar-preview
		job := _job
		p := promise.New(func(resolve func(int), reject func(error)) {
			result := job.Value1 + job.Value2
			resolve(result)
		})
		promises = append(promises, p)
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	values, err := promise.MergeAll(ctx, promises...).Await(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println(values)
}

Chaining promises

package main

import (
    "time"
    "strconv"
    "context"

    "github.com/4strodev/promise"
)

func main() {
	p := promise.New(func(resolve func(int), reject func(error)) {
		time.Sleep(time.Millisecond * 1)
		resolve(1)
	})
	ctx := context.Background()
	newPromise := promise.Then(ctx, p, func(num int) string {
		time.Sleep(time.Millisecond * 1)
		return strconv.Itoa(num * 2)
	})
	value, err := newPromise.Await(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println(value)
	// Output: 2
}

Suggestions are accepted

It does not mean that all suggestions will apply. It means that the suggestions will be read and evaluated. Feel free to make any PR or Issue. Obviously, keep in mind that this is a personal project. Which I made open source under the MIT license. I don't have much time, but I promise I will be checking the repository ๐Ÿ˜‰.

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.