{"id":21966693,"url":"https://github.com/4strodev/promise","last_synced_at":"2025-04-24T05:10:18.993Z","repository":{"id":195583218,"uuid":"693219160","full_name":"4strodev/promise","owner":"4strodev","description":"A simple go library to manage async operations using promises","archived":false,"fork":false,"pushed_at":"2023-10-06T11:54:45.000Z","size":19,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T05:10:00.223Z","etag":null,"topics":[],"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/4strodev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-18T15:27:49.000Z","updated_at":"2024-09-07T06:20:55.000Z","dependencies_parsed_at":"2023-09-24T01:40:43.685Z","dependency_job_id":null,"html_url":"https://github.com/4strodev/promise","commit_stats":null,"previous_names":["4strodev/promise"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4strodev%2Fpromise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4strodev%2Fpromise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4strodev%2Fpromise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4strodev%2Fpromise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4strodev","download_url":"https://codeload.github.com/4strodev/promise/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250566509,"owners_count":21451232,"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":[],"created_at":"2024-11-29T13:17:50.056Z","updated_at":"2025-04-24T05:10:18.969Z","avatar_url":"https://github.com/4strodev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go Promise package\nA simple library that allows you to create promises with go.\n\n## Installation\n\n```sh\ngo get github.com/4strodev/promise\n```\n\n## Examples\nCreating a promise\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"github.com/4strodev/promise/pkg\"\n\t\"time\"\n)\n\nfunc main() {\n\tp := promise.New(func(resolve func(int), reject func(error)) {\n\t\ttime.Sleep(1 * time.Second)\n\t\tresolve(1)\n\t})\n\n\tresult, err := p.Await(context.Background())\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(result)\n\t// Output: 1\n}\n```\n\nWorking with multiple promises\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"time\"\n\n\t\"github.com/4strodev/promise/pkg\"\n)\n\ntype Job struct {\n\tValue1 int\n\tValue2 int\n}\n\nfunc main() {\n\tjobs := []Job{}\n\tpromises := []*promise.Promise[int]{}\n\tfor i := 0; i \u003c 10; i++ {\n\t\tjob := Job{\n\t\t\tValue1: rand.Intn(10),\n\t\t\tValue2: rand.Intn(10),\n\t\t}\n\t\tjobs = append(jobs, job)\n\t}\n\n\tfor _, _job := range jobs {\n\t\t// For new go developers loop variables are loop scoped\n\t\t// that means that the variable _job is declared only once\n\t\t// and for each iteration the value is overrited\n\t\t// that caused a lot of bugs and will be changed in go 1.22\n\t\t// for more information see https://go.dev/blog/loopvar-preview\n\t\tjob := _job\n\t\tp := promise.New(func(resolve func(int), reject func(error)) {\n\t\t\tresult := job.Value1 + job.Value2\n\t\t\tresolve(result)\n\t\t})\n\t\tpromises = append(promises, p)\n\t}\n\n\tctx, cancel := context.WithTimeout(context.Background(), time.Second)\n\tdefer cancel()\n\n\tvalues, err := promise.MergeAll(ctx, promises...).Await(ctx)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(values)\n}\n```\n\nChaining promises\n```go\npackage main\n\nimport (\n    \"time\"\n    \"strconv\"\n    \"context\"\n\n    \"github.com/4strodev/promise\"\n)\n\nfunc main() {\n\tp := promise.New(func(resolve func(int), reject func(error)) {\n\t\ttime.Sleep(time.Millisecond * 1)\n\t\tresolve(1)\n\t})\n\tctx := context.Background()\n\tnewPromise := promise.Then(ctx, p, func(num int) string {\n\t\ttime.Sleep(time.Millisecond * 1)\n\t\treturn strconv.Itoa(num * 2)\n\t})\n\tvalue, err := newPromise.Await(ctx)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(value)\n\t// Output: 2\n}\n```\n\n## Suggestions are accepted\nIt does not mean that all suggestions will apply. It means that the suggestions will be\nread and evaluated. Feel free to make any PR or Issue. Obviously, keep in mind that this is a personal project.\nWhich I made open source under the MIT license. I don't have much time, but I promise I will be\nchecking the repository 😉.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4strodev%2Fpromise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4strodev%2Fpromise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4strodev%2Fpromise/lists"}