{"id":30940,"date":"2020-12-07T16:30:33","date_gmt":"2020-12-07T21:30:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=30940"},"modified":"2022-05-05T13:49:09","modified_gmt":"2022-05-05T17:49:09","slug":"swift-proposal-for-async-await","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/12\/07\/swift-proposal-for-async-await\/","title":{"rendered":"Swift Proposal for async\/await"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0296-async-await.md\">SE-0296<\/a> (via <a href=\"https:\/\/twitter.com\/dgregor79\/status\/1334983963132063744\">Doug Gregor<\/a>):<\/p>\n<blockquote cite=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/main\/proposals\/0296-async-await.md\"><p>This design introduces a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coroutine\">coroutine model<\/a> to Swift. Functions can opt into being <code>async<\/code>, allowing the programmer to compose complex logic involving asynchronous operations using the normal control-flow mechanisms. The compiler is responsible for translating an asynchronous function into an appropriate set of closures and state machines.<\/p><p>This proposal defines the semantics of asynchronous functions. However, it does not provide concurrency: that is covered by a separate proposal to introduce structured concurrency, which associates asynchronous functions with concurrently-executing tasks and provides APIs for creating, querying, and cancelling tasks.<\/p><p>[&#8230;]<\/p><p>Because only <code>async<\/code> code can call other <code>async<\/code> code, this proposal provides no way to initiate asynchronous code. This is intentional: all asynchronous code runs within the context of a &ldquo;task&rdquo;, a notion which is defined in the <a href=\"https:\/\/github.com\/DougGregor\/swift-evolution\/blob\/structured-concurrency\/proposals\/nnnn-structured-concurrency.md\">Structured Concurrency<\/a> proposal. That proposal provides the ability to define asynchronous entry points to the program via <code>@main<\/code> [&#8230;]<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/11\/03\/swift-concurrency-roadmap\/\">Swift Concurrency Roadmap<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-proposal-for-async-await-update-2020-12-08\">Update (2020-12-08): <a href=\"https:\/\/www.enekoalonso.com\/2020\/12\/06\/getting-started-with-async-await-in-swift.html\">Eneko Alonso<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.enekoalonso.com\/2020\/12\/06\/getting-started-with-async-await-in-swift.html\"><p>The above two extensions allow launching an async task from a <code>DispatchQueue<\/code>, with or without a <code>DispatchGroup<\/code>.<\/p><\/blockquote>\n\n<p id=\"swift-proposal-for-async-await-update-2020-12-24\">Update (2020-12-24): <a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/1341934134021648384\">Ben Cohen<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/AirspeedSwift\/status\/1341934134021648384\">\n<p>The async\/await proposal is <a href=\"https:\/\/forums.swift.org\/t\/accepted-with-modification-se-0296-async-await\/43318\">accepted<\/a>!<\/p>\n<p>The first of a series of Concurrency proposals coming to Swift Evolution. \n@dgregor79\n put together a <a href=\"https:\/\/twitter.com\/AirspeedSwift\/status\/1341934134021648384\/photo\/1\">dependency graph<\/a>&#8230;<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>SE-0296 (via Doug Gregor): This design introduces a coroutine model to Swift. Functions can opt into being async, allowing the programmer to compose complex logic involving asynchronous operations using the normal control-flow mechanisms. The compiler is responsible for translating an asynchronous function into an appropriate set of closures and state machines.This proposal defines the semantics [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-12-07T21:30:36Z","apple_news_api_id":"89762cce-833f-4835-88cf-698bf0a57b05","apple_news_api_modified_at":"2022-05-05T17:49:14Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAg==","apple_news_api_share_url":"https:\/\/apple.news\/AiXYszoM_SDWIz2mL8KV7BQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[800,880,46,71,2200,901],"class_list":["post-30940","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-grand-central-dispatch-gcd","tag-languagedesign","tag-programming","tag-swift-concurrency","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/30940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=30940"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/30940\/revisions"}],"predecessor-version":[{"id":31138,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/30940\/revisions\/31138"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=30940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=30940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=30940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}