Skip to content

Instantly share code, notes, and snippets.

@koher
Created January 27, 2021 09:38
Show Gist options
  • Save koher/ca74ef2c882418f683d06092f4408c3c to your computer and use it in GitHub Desktop.
Save koher/ca74ef2c882418f683d06092f4408c3c to your computer and use it in GitHub Desktop.
// わざわざ 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