{"id":13483785,"url":"https://github.com/anykeyh/await_async","last_synced_at":"2025-04-15T22:53:57.224Z","repository":{"id":69956115,"uuid":"139703334","full_name":"anykeyh/await_async","owner":"anykeyh","description":"Provide await and async methods to Crystal Lang","archived":false,"fork":false,"pushed_at":"2023-09-02T18:32:58.000Z","size":15,"stargazers_count":86,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T22:53:53.015Z","etag":null,"topics":["concurrency","crystal-language"],"latest_commit_sha":null,"homepage":"https://github.com/anykeyh/await_async","language":"Crystal","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/anykeyh.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}},"created_at":"2018-07-04T09:55:29.000Z","updated_at":"2025-02-16T01:53:56.000Z","dependencies_parsed_at":"2024-01-27T09:30:46.173Z","dependency_job_id":null,"html_url":"https://github.com/anykeyh/await_async","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/anykeyh%2Fawait_async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anykeyh%2Fawait_async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anykeyh%2Fawait_async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/anykeyh%2Fawait_async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/anykeyh","download_url":"https://codeload.github.com/anykeyh/await_async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249167439,"owners_count":21223505,"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":["concurrency","crystal-language"],"created_at":"2024-07-31T17:01:15.256Z","updated_at":"2025-04-15T22:53:57.206Z","avatar_url":"https://github.com/anykeyh.png","language":"Crystal","funding_links":[],"categories":["Processes and Threads"],"sub_categories":[],"readme":"## Await / Async\n\n[![Build Status](https://travis-ci.org/anykeyh/await_async.svg?branch=master)](https://travis-ci.org/anykeyh/await_async)\n\nAdd `await` and `async` keywords to Crystal.\n\n## Installation\n\nIn your `shards.yml`:\n\n```yaml\ndependencies:\n  await_async:\n    github: anykeyh/await_async\n    branch: master\n```\n\nThen:\n\n```crystal\nrequire \"await_async\"\n\nfuture = async fetch_something\n\ndo_some_computation_now\n\nawait future\n```\n\n## Usage\n\n- Call `async` on any method or block to create a `MiniFuture`\n- Call `await` on any `MiniFuture` to wait for/get the result\n- Conveniently, you can call `await` on future's array.\n\nCan improve drastically application which relay on blocking IO like web API\nor file writing.\n\n### await(timeout, future)\n\n```crystal\nfuture = async check_website\n\nbegin\n  await 5.seconds, future\nrescue MiniFuture::TimeoutException\n  # rescue from timeout\nend\n```\n\n### `async!` / `async`\n\nBy default, `async!` call the newly created fiber just after creation.\n\n- You can use instead `async` so the fiber won't start now:\n\n```crystal\nfuture = async! { 1 + 2 }\n# At this moment the result is already computed\n# future.finished? == true\nawait future # =\u003e 3\n\n# vs\n\nfuture = async { 1 + 2 }\n# Here the result is not computed\n# future.finished? == false\nawait future # Compute now\n```\n\nUsually, use `async` if your block is computation intensive and current thread\nhas IO blocking operation. Use `async!` in other cases.\n\nIn case of errors, the exception will be raise at `await` moment, in the await\nthread.\n\n## `MiniFuture`\n\nA minimalist version of future. Has `finished?` and `running?` methods.\n\nI don't use Crystal's `Concurrent::Future` class because `:nodoc:`.\n\n## Why?\n\nBecause crystal is great for building CLI tools. And CLI deals a lot with\nfiles and sockets. And IO performed in main thread are slow.\n\nUsage of `Channel` is recommended for complex software, as it offers more patterns.\n\n`await/async` is useful to build fast and deliver fast.\n\n## I don't want await/async to be exported in the global scope\n\n1. require `await_async/helper` instead of `await_async`\n2. In the class/module you want to use the methods, add `include AwaitAsync::Helper`.\n   You can also simply call `await/async` directly from `AwaitAsync::Helper`\n\n## Example\n\n```crystal\ndef fetch_websites_async\n  %w[\n    www.github.com\n    www.yahoo.com\n    www.facebook.com\n    www.twitter.com\n    crystal-lang.org\n  ].map do |url|\n    async! do\n      HTTP::Client.get \"https://#{url}\"\n    end\n  end\nend\n\n# Process the websites concurrently. Start querying another website when the\n# first one is waiting for response\nawait(5.seconds, fetch_websites_async).each do |response|\n  # ...\nend\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanykeyh%2Fawait_async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanykeyh%2Fawait_async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanykeyh%2Fawait_async/lists"}