{"id":9218,"date":"2023-12-08T21:32:03","date_gmt":"2023-12-08T21:32:03","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9218"},"modified":"2023-12-08T21:32:03","modified_gmt":"2023-12-08T21:32:03","slug":"swift-async-await","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/swift-async-await\/","title":{"rendered":"Swift async\/await"},"content":{"rendered":"<p><em>Note: I\u2019m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never.<\/em><\/p>\n<p>Swift comes with async\/await syntax\/functionality, however the support is dependent upon version of Swift and more importantly whether it&#8217;s the Mac (I&#8217;ll include iOS and watch OS in this) version of the Linux version.<\/p>\n<p>Currently I&#8217;m playing with Swift on Linux and using the <em>URLSession.shared.dataTask<\/em> which is essentially a callback function. The <em>URLSession.shared.data<\/em> async\/await version currently does not exist on Linux. So let&#8217;s create our own version and look at the process and ofcourse, how to use async\/await (if you&#8217;ve used C#, TypeScript with async\/await you&#8217;ll already know the basics of using the syntax).<\/p>\n<p>Functions should be declared as async, so for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nfunc data(for request: URLRequest) async -&gt; (Data, URLResponse) {\r\n  \/\/ do something\r\n}\r\n<\/pre>\n<p>Here&#8217;s an example of using the <em>URLSession.shared.dataTask<\/em> and declaring the code in an async method<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n#if canImport(FoundationNetworking)\r\npublic extension URLSession {\r\n    func data(for request: URLRequest) async -&gt; (Data, URLResponse) {\r\n        await withCheckedContinuation { continuation in\r\n            URLSession.shared.dataTask(with: request) { data, response, error in\r\n                continuation.resume(returning: (data!, response!))\r\n            }.resume()\r\n        }\r\n    }\r\n}\r\n#endif\r\n<\/pre>\n<p>Now, in usage, we can do this<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nlet data = await URLSession.shared.data(for: request)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note: I\u2019m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never. Swift comes with async\/await syntax\/functionality, however the support is dependent upon version of Swift and more importantly whether it&#8217;s the Mac (I&#8217;ll include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9218","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9218"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9218\/revisions"}],"predecessor-version":[{"id":10319,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9218\/revisions\/10319"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}