Especially when you are coming from Java word, the first encounter with asynchronous style used in Javascript (or node.js) can be confusing. And of course there are several ways how to deal with it.

  • Callback hellAs also for other styles, modularization and good names helps in readability.
  • Async libraryThis library helps with increasing intendations and covers almost any use case. It is very useful at least to be aware of their options.
  • Promises (ES6)It is different style then node.js (default) error first callbacks. Working with promises feels more natural (you get “async” result as result of function instead of passing callback argument, but YMMV). However for this reason this approach does not work directly with node.js API. You need to wrap the code or use other solutions like es6-promisify (which can have some limitations)Another problem is inconsistent error handling however in node.js and chrome it seems to work fine.
  • async/await (ES7)Still not standardized, you need babel or similar solution to make it working. Similar style to promises. We had some problems when we tried to use in in some high-order functions (but can’t remember anymore).

Till now there is no obvious winner and maybe future will show.

Related Post

Leave a Comment

© 2024 Instea, s.r.o.
All rights reserved. Privacy policy

Contact us

Where to find us

Connect with us