In the previous article, we learned about suspended functions provided by coroutines and the value they bring to the code. We also learned how coroutine and suspended functions work hand in hand to bridge threads and execute asynchronous tasks without us having to create notification mechanisms. Moving forward one of the major challenges we all have faced in java while dealing with asynchronous programming is being able to return values from a function that computes on a separate thread. The system will never know when to return as there is no mechanism existing to coordinate between two different threads. Programmers from different domains have developed several mechanisms to handle such situations. Let’s dive in to know more about them briefly.

Promising values in javascript

A promise construct is just what the name says — a promise get a value back, which may or may not be present. They work by taking a function call and storing the respective object in a construct. One of the magical features provided here is that these promises can be very elegantly chained to other promises. We have two main functions here _then() _and _catch(). _The former one does the job of returning a value or chaining the value to other promises while the later one is a fallback function catching errors if encountered.

#kotlin-coroutines #asyncawait #android-app-development #kotlin

Kotlin Coroutines-Async/Await
1.90 GEEK