bene : studio is a global consultancy, helping startups, enterprises and HealthTech companies to have better product
Async/await vs Coroutines vs Promises vs Callbacks
Our workshops are restarting!
Join the FREE ONLINE React Native workshop on August 11, and learn how to connect native modules to build an awesome Music Creator.
Join the workshop and invite your peers as well! For full details and registration visit the event page:
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 email@example.com 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 Christmas chat tree
Or should we say Chat-mas tree?😜 Create a working chat application in 2 hours using React, SSE (Server-Sent Events) and OneSignal on Heroku.
5 Ways to Connect Redux Actions
This article provides 5 ways of defining mapDispatchToProps with explanations. Great introduction to our Redux Saga Workshop!
Bene Studio Serverless Workshop - Calorie Counter App
Kickstart your React Native app with Firebase – Cloud Firestore (Tutorial #2)
In this series we will create an iOS and Android application — called Fakestagram — with React Native, using some of Firebase services....
Redux-Saga To The Rescue
In this blog post, we are checking back to our 25. September Redux Saga Workshop. On 19. October we are trying out a new JS Framework for cross-platform...