Hacker News new | past | comments | ask | show | jobs | submit login

Because JS is single-threaded¹ and is designed for use on the web. If you block that thread, then all event processing ceases—button clicks don't register, scroll events don't fire, etc. So all JS API's are designed to be non-blocking.

¹Not really, but close enough.

Originally, this was resolved via callbacks:

  doSomething((err, result) => {
    if (err) { ...failure code... }
    ...success code...
  }
But that got very clunky, resulting in something called "callback hell" and the "callback pyramid of doom." So promises were introduced:

  doSomething().then((result) => {
    ...success code...
  }).catch((err) => {
    ...failure code...
  });
But that's also clunky, so the async/await syntax was introduced:

  try {
    let result = await doSomething();
    ...success code...
  }
  catch (err) {
    ...failure code...
  }
And that's actually quite pleasant to use.



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: