Hacker News new | ask | show | jobs
by thomasfoster96 4044 days ago
Indeed, I've noticed that quite a few -Sync methods aren't documented anymore. My hope is that node will have a major release which removes all -Sync methods and replaces callbacks with Promises. Then we'll all be happy and use cluster when we can.
1 comments

No please! More sync function please!

Yes, if I'm writing a web server I want everything to be async. But, just as I want to share JS on the server and the client I also want to use JS as my build language. At least for me, I find it much faster to build using a sync style.

Maybe I just haven't learned the async way but for example I tried to make a build system using node. I needed to spawn out to a builder to build some stuff, copy files, spawn git in various ways to see if repos are dirty or clean, git add, git commit, and a few other things. I found it a massive nightmare and after 2 days I switched to synchronous python for my building. Was done in 2 hours.

If there's some articles or tips that will make me as comfortable at async for building as I'm as sync in python then please point me at them. But, for whatever reason, I'm struggling with async for really complex tasks. (and yes, I'm using promises)

If you truly need to do sync programming for whatever reason, you probably aren't using the right language. It's not just web programming that benefits from asynchrony, it's pretty much any non-trivial algorithm. Sure, it can require a bit more code to do right, but you don't want to block all your other background events from firing just because you have to write a bit more code.

If you really prefer synchronous style, and don't mind the drawbacks, there are plenty of languages out there that'll work better for you. PHP, Ruby, and Python readily come to mind.

Yes, but a big draw of using node at all is that it's JavaScript. I write some function "templateThisString" and I can use it on both the server and the client. Now I also want to use it in my build process. If I switch to another language I have double the work.
I actually have a tip for async programming on small projects. Consider storing as much of your state as possible in a central location if it's feasible for your application. As much as possible, avoid relying on closures except for data hiding. This makes it easier to think about what is going on in your application even if the code flow is not exactly linear.

For example use a global object called State that contains information about the list of build tasks to be run and the state of each of the stages for the tasks.

Well, okay, quick and dirty scripts where you are expecting (or want) things to be blocking are probably the one place where you need things to be sync.

I suppose though that you could always just call process.exit() when a callback was called or a promise resolved. Personally I'd love a file-level await statement.