{"id":15034596,"url":"https://github.com/chebyrash/promise","last_synced_at":"2025-05-15T09:03:59.995Z","repository":{"id":29907432,"uuid":"120822174","full_name":"chebyrash/promise","owner":"chebyrash","description":"Promise / Future library for Go","archived":false,"fork":false,"pushed_at":"2023-07-09T13:38:09.000Z","size":157,"stargazers_count":398,"open_issues_count":0,"forks_count":36,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-31T02:11:56.364Z","etag":null,"topics":["future","futures","go","go-generics","golang","golang-examples","golang-future","golang-library","golang-package","goroutine","goroutine-pool","pool","promise","promise-library","promises","promises-library","worker-pool"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chebyrash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-08T21:49:21.000Z","updated_at":"2025-03-12T14:12:35.000Z","dependencies_parsed_at":"2024-06-18T13:40:31.068Z","dependency_job_id":"9f5c465a-336f-4f35-b698-323fc86448f6","html_url":"https://github.com/chebyrash/promise","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chebyrash%2Fpromise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chebyrash%2Fpromise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chebyrash%2Fpromise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chebyrash%2Fpromise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chebyrash","download_url":"https://codeload.github.com/chebyrash/promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247584111,"owners_count":20962075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["future","futures","go","go-generics","golang","golang-examples","golang-future","golang-library","golang-package","goroutine","goroutine-pool","pool","promise","promise-library","promises","promises-library","worker-pool"],"created_at":"2024-09-24T20:25:39.154Z","updated_at":"2025-04-07T03:17:35.854Z","avatar_url":"https://github.com/chebyrash.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# PROMISE\n[![Go Report Card](https://goreportcard.com/badge/github.com/chebyrash/promise)](https://goreportcard.com/report/github.com/chebyrash/promise)\n[![Build Status](https://github.com/chebyrash/promise/actions/workflows/test.yml/badge.svg)](https://github.com/chebyrash/promise/actions)\n[![Go Reference](https://pkg.go.dev/badge/github.com/chebyrash/promise.svg)](https://pkg.go.dev/github.com/chebyrash/promise)\n\n## Introduction\n\n`promise` allows you to write async code in sync fashion\n\n- First class [context.Context](https://blog.golang.org/context) support\n- Automatic panic recovery\n- Generics support\n- Goroutine pool support\n\t- [sourcegraph/conc](https://github.com/sourcegraph/conc)\n\t- [panjf2000/ants](https://github.com/panjf2000/ants)\n\t- Your own!\n\n## Install\n\n    $ go get github.com/chebyrash/promise\n\n## Quickstart\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/chebyrash/promise\"\n)\n\nfunc main() {\n\tp1 := promise.New(func(resolve func(int), reject func(error)) {\n\t\tfactorial := findFactorial(20)\n\t\tresolve(factorial)\n\t})\n\tp2 := promise.New(func(resolve func(string), reject func(error)) {\n\t\tip, err := fetchIP()\n\t\tif err != nil {\n\t\t\treject(err)\n\t\t} else {\n\t\t\tresolve(ip)\n\t\t}\n\t})\n\n\tfactorial, _ := p1.Await(context.Background())\n\tfmt.Println(*factorial)\n\n\tIP, _ := p2.Await(context.Background())\n\tfmt.Println(*IP)\n}\n\nfunc findFactorial(n int) int {\n\tif n == 1 {\n\t\treturn 1\n\t}\n\treturn n * findFactorial(n-1)\n}\n\nfunc fetchIP() (string, error) {\n\tresp, err := http.Get(\"https://httpbin.org/ip\")\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tdefer resp.Body.Close()\n\n\ttype Response struct {\n\t\tOrigin string `json:\"origin\"`\n\t}\n\tvar response Response\n\n\terr = json.NewDecoder(resp.Body).Decode(\u0026response)\n\treturn response.Origin, err\n}\n```\n\n## Pool\n\n- Promise execution can be dispatched to distinct pools, providing granular control over task distribution and concurrency.\n\n- Better performance can be achieved by allowing different stages of a Promise chain to be executed on different goroutine pools, optimizing for the specific requirements of each task.\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\n\t\"github.com/chebyrash/promise\"\n)\n\nfunc main() {\n\tctx := context.Background()\n\n\t// fetches data from API, runs on ioOptimizedPool\n\tdataPromise := promise.NewWithPool(func(resolve func(string), reject func(error)) {\n\t\tdata, err := fetchDataFromAPI()\n\t\tif err != nil {\n\t\t\treject(err)\n\t\t} else {\n\t\t\tresolve(data)\n\t\t}\n\t}, ioOptimizedPool)\n\n\t// computes result based on the fetched data, runs on cpuOptimizedPool\n\tresultPromise := promise.ThenWithPool(dataPromise, ctx, func(data string) (string, error) {\n\t\tresult, err := computeResult(data)\n\t\treturn result, err\n\t}, cpuOptimizedPool)\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchebyrash%2Fpromise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchebyrash%2Fpromise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchebyrash%2Fpromise/lists"}