{"id":25193,"date":"2019-05-08T19:17:44","date_gmt":"2019-05-08T23:17:44","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=25193"},"modified":"2019-05-08T19:18:17","modified_gmt":"2019-05-08T23:18:17","slug":"optional-throws-result-and-asyncawait","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2019\/05\/08\/optional-throws-result-and-asyncawait\/","title":{"rendered":"Optional, throws, Result, and async\/await"},"content":{"rendered":"<p><a href=\"https:\/\/nshipster.com\/optional-throws-result-async-await\/\">Joshua Emmons<\/a>:<\/p>\n<blockquote cite=\"https:\/\/nshipster.com\/optional-throws-result-async-await\/\">\n<p>Ah ha! So we see that the <code>Result<\/code> type can serve as a concrete <a href=\"https:\/\/en.wikipedia.org\/wiki\/Reification_%28computer_science%29\">reification<\/a> of Swift&rsquo;s abstract idea of <em>&ldquo;that thing that&rsquo;s returned when a function is marked as <code>throws<\/code>.&rdquo;<\/em> And as such, we can use it to deal with asynchronous operations that require concrete types for parameters passed to their completion handlers.<\/p><p>[&#8230;]<\/p> <p>Thankfully, we can clean this up by taking advantage of the fact that, like <code>Optional<\/code>, <a href=\"https:\/\/github.com\/apple\/swift\/blob\/swift-5.0-RELEASE\/stdlib\/public\/core\/Result.swift#L96\"><code>Result<\/code> implements <code>flatMap<\/code><\/a>. Specifically, <code>flatMap<\/code> on a <code>Result<\/code> will, in the case of <code>.success<\/code>, apply the given transform to the associated value and return the newly produced <code>Result<\/code>. In the case of a <code>.failure<\/code>, however, <code>flatMap<\/code> simply passes the <code>.failure<\/code> and its associated error along without modification.<\/p><p>[&#8230;]<\/p> <p>In the near term, we just have to lump it.\n  It&rsquo;s better than the other alternatives native to the language,\n  and chaining asynchronous calls isn&rsquo;t as common as for synchronous calls.<\/p>\n<p>But in the future, just as Swift used <code>do\/catch<\/code> syntax to define away <code>Result<\/code> nesting problems in synchronous error handling, there are many proposals being considered to do the same for asynchronous errors (and asynchronous processing, generally).<\/p>\n<\/blockquote>\n\n<p>See also: <a href=\"https:\/\/nshipster.com\/swift-foundation-error-protocols\/\">Localized&#x200B;Error, Recoverable&#x200B;Error, Custom&#x200B;NSError<\/a>.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/18\/concurrency-in-swift-one-possible-approach\/\">Concurrency in Swift: One Possible Approach<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/08\/29\/error-handling-compared\/\">Error Handling Compared<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/06\/19\/swift-2-error-handling-continued\/\">Swift 2 Error Handling, Continued<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Joshua Emmons: Ah ha! So we see that the Result type can serve as a concrete reification of Swift&rsquo;s abstract idea of &ldquo;that thing that&rsquo;s returned when a function is marked as throws.&rdquo; And as such, we can use it to deal with asynchronous operations that require concrete types for parameters passed to their completion [&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":"2019-05-08T23:17:47Z","apple_news_api_id":"50df50ab-6f75-4641-9fb9-ed7a4b5dd469","apple_news_api_modified_at":"2019-05-08T23:18:21Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AUN9Qq291RkGfue16S13UaQ","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":[46,857,71,901],"class_list":["post-25193","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-nserror","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/25193","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=25193"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/25193\/revisions"}],"predecessor-version":[{"id":25195,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/25193\/revisions\/25195"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=25193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=25193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=25193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}