Created
January 27, 2021 09:38
-
-
Save koher/ca74ef2c882418f683d06092f4408c3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// わざわざ async let を使って代入を挟まなくても | |
// 非同期処理を↓のようにして並行に実行できるようにするのは | |
// | |
// await (foo() + bar()) // 並行 | |
// (await foo()) + (await bar()) // 直列 | |
// | |
// try との整合性の関係で難しそう。 | |
// | |
// try の仕様が、↓の (A) では "foo" と "bar" が、 | |
// (B) では "foo" のみが表示されるようになっていれば | |
// それと整合性を保つ形で↑のように並行と直列を扱えた。 | |
struct MyError: Error {} | |
func foo() throws -> Int { | |
print("foo") | |
throw MyError() | |
} | |
func bar() throws -> Int { | |
print("bar") | |
throw MyError() | |
} | |
do { // (A) | |
print(try (foo() + bar())) | |
} catch { | |
print(error) | |
} | |
do { // (B) | |
print((try foo()) + (try bar())) | |
} catch { | |
print(error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment