The async/await feature is not released yet, APIs might change or break. Please don't use this code in production until the feature branches are merge

Beginner's guide to the async/await concurrency API in Vapor & Fluent

submited by
Style Pass
2021-06-17 15:30:05

The async/await feature is not released yet, APIs might change or break. Please don't use this code in production until the feature branches are merged. This experimental feature will be available later on this year with the official Swift 5.5 and Vapor 4 releases.

So you might wonder why do we even need to add async/await support to our codebase? Well, let me show you a dirty example from a generic controller inside the Feather CMS project.

What do you think? Is this code readable, easy to follow or does it look like a good foundation of a historical monumental building? Well, I'd say it's hard to reason about this piece of Swift code. 😅

I'm not here to scare you, but I suppose that you've seen similar (hopefully more simple or better) EventLoopFuture-based code if you've worked with Vapor. Futures and promises are just fine, they've helped us a lot to deal with asynchronous code, but unfortunately they come with maps, flatMaps and other block related solutions that will eventually lead to quite a lot of trouble.

We can say it's easy to make mistakes if it comes to completion handlers, that's why we have a shiny new feature in Swift 5.5 called async/await and it aims to solve these problems I mentioned before. If you are looking for an introduction to async/await in Swift you should read my other tutorial first, to learn the basics of this new concept.

Leave a Comment