Send Emails with SendGrid API using Micronaut with Kotlin Coroutines

Ubed Ali

What is Coroutine?

From Kotlin docs:

One can think of a coroutine as a light-weight thread. Like threads, coroutines can run in parallel, wait for each other and communicate. The biggest difference is that coroutines are very cheap, almost free: we can create thousands of them, and pay very little in terms of performance. True threads, on the other hand, are expensive to start and keep around. A thousand threads can be a serious challenge for a modern machine.

SEO stands for Search Engine Optimisation, which is the practice of optimising your website and increasing the quantity and quality of traffic to your website through organic search engine results.

So why Kotlin Coroutines?

A coroutine is an instance of suspendable computation. It is conceptually similar to a thread, in the sense that it takes a block of code to run that works concurrently with the rest of the code. However, a coroutine is not bound to any particular thread. It may suspend its execution in one thread and resume in another one.

  • Coroutines help to manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive.

  • They are light-weight

You’re expected to be familiar with basic Kotlin syntax, but prior knowledge of coroutines is not required.

Coroutines basics

This section covers basic coroutine concepts.

Your first Coroutine

Coroutines can be thought of as light-weight threads, but there is a number of important differences that make their real-life usage very different from threads.

Run the following code to get to your first working coroutine: