Skip to content

Comment on Escape from Callback Hell

Comments

It seems like this problem would be elegantly solved by starting a thread, green thread or coroutine (depending on language) for each task and calling the API functions synchronously from within that. I'm not sure what support JS has for these things.

Precisely. Threads allow you to separate concerns better - one thread per task, rather than trying to process all tasks.

Simon Marlow captured this well: http://stackoverflow.com/a/3858684/83805

"the abstractions that threads provide are essential for making server code easier to get right, and more robust"

JS has no support for these things, but you're right, when using languages (like Go) that do support this concept, it is very easy to write elegant code which can block locally without actually blocking the entire application, which allows you to write code that is far cleaner and easier to reason about.

The problem is javascript doesn't have support for this. That's why we see callbacks everywhere.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.