{"id":21376521,"url":"https://github.com/fluidgroup/swift-async-timeout","last_synced_at":"2025-07-13T10:31:06.287Z","repository":{"id":198345560,"uuid":"700626787","full_name":"FluidGroup/swift-async-timeout","owner":"FluidGroup","description":"an api that occurs timeout and cancel enclosed tasks then moving on the next steps.","archived":false,"fork":false,"pushed_at":"2023-10-23T19:00:03.000Z","size":9,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-15T01:19:08.283Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://muukii.framer.website/articles/timeout-operation-swift-concurrency","language":"Swift","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/FluidGroup.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}},"created_at":"2023-10-05T01:00:05.000Z","updated_at":"2024-04-11T00:19:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"3ab58a27-4fcd-4ee0-bd48-c00243a94f1a","html_url":"https://github.com/FluidGroup/swift-async-timeout","commit_stats":null,"previous_names":["muukii/swift-async-timeout","fluidgroup/swift-async-timeout"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/FluidGroup/swift-async-timeout","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-async-timeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-async-timeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-async-timeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-async-timeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FluidGroup","download_url":"https://codeload.github.com/FluidGroup/swift-async-timeout/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2Fswift-async-timeout/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265128202,"owners_count":23715621,"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-22T09:16:11.121Z","updated_at":"2025-07-13T10:31:06.057Z","avatar_url":"https://github.com/FluidGroup.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## Understanding Task Cancellation\n\nIt's crucial to note that canceling tasks does not equate to halting ongoing operations. Instead, it informs the tasks that they are canceled. The following code illustrates this concept by canceling a task. Notably, the \"done\" print statement is executed only after the longOperation has completed, unless the operation includes specific handling for cancellation.\n\n```swift\nlet task = Task {\n  await longOperation()\n  print(\"done\")\n}\n...\n\ntask.cancel()\n```\n\nHandling Timeouts\nConsider the following scenario:\n\n```swift\nawait fetchFlag() // A timeout of 5 seconds is desired.\napplyFlag()\n```\n\nIn this case, we require a mechanism to enforce a timeout of 5 seconds when fetching the flag, regardless of whether triggering a cancelation genuinely halts the ongoing operation and discards its progress. The behavior we aim to achieve is to time out the operation at all costs and proceed with subsequent steps. This means that the fetch request may still be in progress, but the program will move forward due to the timeout.\n\nImplementing a Timeout with withTimeout\nTo achieve this specific timeout behavior, the withTimeout function is introduced, which utilizes unstructured concurrency and error handling. Here's how it works:\n\n```swift\nawait withTimeout(5) {\n  await fetchFlag() // This is expected to time out in 5 seconds.\n}\n\napplyFlag() // Apply a new flag or the current flag if a timeout occurred.\n```\n\nThe withTimeout function allows you to specify a time limit (in nanoseconds) and execute a block of code within that time frame. If the specified time elapses, the function cancels the associated task, ensuring that the program proceeds without waiting for the task's completion. The applied flag can then be updated based on whether a timeout occurred or not.\n\nThis approach provides a powerful mechanism for managing timeouts in asynchronous code, offering fine-grained control over task cancellation and ensuring that your application remains responsive even in the face of potential delays.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluidgroup%2Fswift-async-timeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluidgroup%2Fswift-async-timeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluidgroup%2Fswift-async-timeout/lists"}