DEV Community

Discussion on: Async done simple

Collapse
 
lucretius profile image
Robert Lippens

An interesting take on the wonderful async world of JavaScript. I have a few questions:

1) I know this was your preference, but why the desire to make JavaScript appear more object-oriented? It's always struck me as a more functional language, with very language-important features like closures for example.

2) why use promises alone when ES6 introduced generators and the latest versions introduced async/await - which can be transpired down with babel on the frontend or used with the right version of Node on the backend? They both make working with async code very nice.

Collapse
 
petarprok profile image
Petar Prokopenko

Hello Robert and thank you for reading my article.

Answers to your questions:

1) The beauty of javascript is that every function you create can be assigned to object and passed as reference for callback functions when you are using express routes. Example: you have routes in one file and ctrl in other. By exporting function in ctrl and requiring ctrl file in routes you can pass ctrl fun in route.
app.get('/', ctrl.testFunction);

2) Yes you can use async/awit. Not everyone uses babel compiler, most of the projects now days are using reactJS, angular, etc. Async/await for node is supported in 7.6 version and if you have project that uses older version you can't do that.

I made this package simple and small, so you can use it in any framework just with couple of lines you can do you async things. Async/awit is good, but most
of devs are still promises. If I read correctly async/awit is new thing from ES8?

Happy coding.

Collapse
 
lucretius profile image
Robert Lippens

Ah so with objects you were referring more to what you’d do with module exports? I can’t disagree with that as that’s exactly what I do. Within those modules though , you could make other functions external to your exports which you could pass as function arguments or use internally (mimicking the idea of private functions)

Async await are available in node 7.6.0+ but generators have been around for a while longer. I think Node 8.5 just got released. Most of my JS work these days is on the backend so I haven’t needed Babel in a while but for a plain JS solution that runs on all browsers I’d say native promises or a wrapper like you’ve made are great choices!