[swift-evolution] [Concurrency] do async

Jonathan Hull jhull at gbis.com
Tue Aug 22 01:22:42 CDT 2017


I have seen a lot of examples which use both do and beginAsync:

	beginAsync {
		do {
			try await foo()
		} catch let e {
			//Handle Error
		}
	}

I twitch every time I see this, because I thought part of the point of this was to avoid pyramiding.  It would seem to be an argument for combining try and await, but as others have pointed out, that causes issues with try? and try!.  I also really like the explicitness of knowing what could actually throw errors.

I propose that we instead combine do and beginAsync.  There are 3 cases:


1) Just throws (i.e what we have now):

	do {
		try foo()
	} catch let e {
		//Handle Error
	}


2) Just async (no catch needed):

	do async {
		await foo()
	}


3) Both throws and async:

	do async {
		try await foo()
	}catch let e{
		//It would be a compiler error not to have a catch statement when there is a try in the do block.
	}


This feels much less messy to me, and will avoid unnecessary pyramids while still allowing throws and async to be separately declared.

Thanks,
Jon






More information about the swift-evolution mailing list