bene : studio is a global consultancy, helping startups, enterprises and HealthTech companies to have better product
Async/await vs Coroutines vs Promises vs Callbacks
At bene : studio we love knowledge sharing to help the community of professionals. With 10+ years and over 100 projects behind us, we have a vast amount of experience. This is why we have launched a knowledge base on our blog with regular updates of tutorials, best practices, and open source solutions.
These materials come from our internal workshops with our team of developers and engineers.
callbacks related? Which one is better or worse? We are going to implement the same code with these 4 different approaches.
First of all, let’s define a simple
delay function, that resolves with a given value after a given amount of time elapsed.
This is the most recent feature of all 4 approaches.
- Clean code, low syntax noise
- Native in current Chrome, Node.js
- Still needs to be transpiled with Babel for most projects
Async Code With Promise Chaining
This is more oldschool than
- No extra libraries needed since Promises are native
- Noisy callbacks
Async Code With Callbacks
Years ago this was the usual way to handle async continuations.
- Easy to understand
- No libs needed, backward compatible
- Pyramid of doom
- More difficult to maintain
Async Code With Coroutines
This is the most exotic one, having similar syntax to
- No pyramid of doom
- Flow can be traced easily
- Some coroutine library is needed
- Generators must be used
If you are curious how a simple coroutine runner works, here we go. It’s just a simplified implementation to have an understanding of the concept.
Conclusions & Opinions
In most cases
async/await is the best choice, since it makes the code maintainable and it’s supported by Node.js and modern browsers. Older browsers or Node.js versions can be targeted by Babel transpiler.
Some folks like to write callback hells and pyramid of dooms. It’s time for them to move on and practise coding with
Of course, without a deep knowledge of Promises,
async/await or coroutines are just magic, so it is very valuable to have a close friendship with Promises.
Did you like this? Join us!
Want to ask some questions? Share them with us via e-mail to firstname.lastname@example.org and we can set up a talk with our engineers.
Fancy working for the studio where you can work on international projects and write articles like this? Check out our open positions!
Looking for a partner for your next project? Check out our services page to see what we do and let’s set up a free consultation.
Read moreSee all software engineering
Build your own neural network using Machine Learning & TensorFlow
Welcome to the exciting world of machine learning! If you missed attending our workshop in-person, don't worry; we've got you covered.
AR and VR apps in healthcare: A glimpse into the future
How will AR & VR techologies revolutionize healthcare? We summarized what to expect in the near future.
Using MongoDB App Services in a React Native app
We are going to create a color set editor app where users can create their own color sets, modify their colors and share it with other users.
In this tutorial, I would like to show the benefits of using monorepos for a full-stack project.
Workshop Recap: connect native modules to build an awesome music creator
Learn, how to work with Native Modules (both Java and Swift) and connect them with React Native.
Let bene : studio enhance
your digital product!