Skip to content

jamestack/promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Promise

A Promise library for Go

  1. any value returned as "Promise" is "PromiseFunc".
  2. support promise chain call.
  3. support err catch.

Example:

package main

import (
	"fmt"
	"github.com/jamestack/promise"
	"time"
)

func Hello(num int64) promise.Promise {
	p := promise.New()
	time.AfterFunc(3*time.Second, func() {
		if num%2 == 0 {
			p.Resolve(num, "yes success")
		} else {
			p.Reject("oh no fail")
		}
	})
	return p
}

func main() {
	fmt.Println("start call")

	Hello(time.Now().Unix()).Then(func(num int64, msg string) promise.Promise {
		fmt.Println("call done: ", num, msg)
		return promise.WithResolve("a", "b")
	}).Then(func(a, b string) promise.Promise {
		p := promise.New()
		time.AfterFunc(3*time.Second, func() {
			p.Resolve()
			fmt.Println("looks good.", a, b)
		})
		return p
	}).Catch(func(errs ...promise.Any) {
		fmt.Println("catch:", errs)
	})

	fmt.Println("end call")
	<-time.After(5 * time.Second)
}

About

Promise library for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages