Comparison

At this point, we should have a better understanding of the options we have to tame the asynchronous nature of JavaScript. Each one of the solutions presented has its own pros and cons. Let's summarize them in the following table:

Solutions

Pros

Cons

Plain JavaScript

  • Does not require any additional libraries or technology
  • Offers the best performance
  • Provides the best level of compatibility with third-party libraries
  • Allows the creation of ad hoc and more advanced algorithms

Might require extra code and relatively complex algorithms

Async (library)

  • Simplifies the most common control flow patterns
  • Is still a callback-based solution
  • Good performance

  • Introduces an external dependency
  • Might still not be enough for advanced flows

Promises

  • Greatly simplifies the most common control flow patterns
  • Robust error handling
  • Part of the ES2015 specification
  • Guarantees deferred invocation of onFulfilled and onRejected

  • Requires promisify callback-based APIs
  • Introduces a small performance hit

Generators

  • Makes non-blocking API look like a blocking one
  • Simplifies error handling
  • Part of ES2015 specification

  • Requires a complementary control flow library
  • Still requires callbacks or promises to implement non-sequential flows
  • Requires thunkify or promisify nongenerator-based APIs

Async await

  • Makes non-blocking API look like blocking
  • Clean and intuitive syntax

  • Not yet available in JavaScript and Node.js natively
  • Requires Babel or other transpilers and some configuration to be used today

Note

It is worth mentioning that we chose to present in this chapter only the most popular solutions to handle asynchronous control flow, or the ones receiving a lot of momentum, but it's good to know that there are a few more options you might want to look at, for example, Fibers (https://npmjs.org/package/fibers) and Streamline (https://npmjs.org/package/streamline).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.216