5 Reasons to Avoid Deasync for Node.js

(Last Updated On: 2019-03-09)

If a Node.js library offers synchronous methods that are not just wrappers for the few synchronous methods offered in the standard library, it probably uses deasync or relies on something that does.  At a glance, it sounds like exactly the thing that every callback weary Node.js developer is looking for.  A way to make an asynchronous function behave synchronously.

1.  The original author, current maintainer, and the maintainers of Node.js recommend against using it.

Deasync might sound like the perfect solution, however, even the current maintainers of deasync recommend that you don’t use it if there is an alternative, which there is in every case.  Not only that, the maintainers of Node.js have deemed deasync unsafe 1 and the original author states in the readme for the library that “[deasync is] just a hack and I’d strongly recommend not to use it for anything.” 2  Unbelievably, deasync is currently at a 140,000 downloads a week and climbing.

2.  Its behavior is not stable and not supported.

Deasync blocks the event loop by calling the Node.js event loop at the JavaScript layer (inside the main event loop).  This is done by exposing functionality from libuv and v8 to the consumer.  Both modules are non-ABI stable, meaning their behavior could change at any point.  This is exactly what happened in 2015, when a user reported that using deasync caused their app to hang in an asynchronous context. 3  Many other users chimed in to say they were having similar problems.

This prompted the maintainer of deasync to create an issue and pull request against Node.js.  The maintainers were quick to point out that what deasync does is not supported.

Oop. I just realized you’re running the event loop within the same event loop. Yeah, that’s definitely not supported by libuv. Sorry. 4

There is no way to reproduce it using public APIs, so it’s not bug. What deasync does is not supported 5

Later on, the maintainers of Node.js declared that deasync is unsafe in addition to being unsupported and the pull request was closed without being merged.

Using deasync is quite literally UNSAFE. 6

3.  It can cause your app to hang indefinitely.

It possible for “deasynced” functions to hang indefinitely, stalling the app.  This is particularly noticeable when a deasynced function is called after trapping process.exit().  Instead of exiting, the program will hang and will not accept additional sigint/sighup signals, requiring the user to find and kill the process with more drastic measures. 7

One of my first ever projects as a software engineer was writing a module that used etcd v3 as a persistent state store for hubot’s brain.  This module was based on @meaballhat’s (Dan Buch) hubot-etcd-brain.  The idea was to tap in to hubot’s event api and listen for save  events emitted by hubot’s brain and transfer the state of hubot’s brain to etcd.

Hubot emits a save event periodically, and most state management libraries for hubot rely on this save event to synchronize the state of the brain to the persistent state store.  The problem with this is that if the state of the brain isn’t changing, hubot is making needless network requests.  To make matters worse, if the save events aren’t frequent enough, there is a risk that data might be lost if the state of the brain changes and app crashes before the next save interval.

I wanted to make sure the state store would attempt to save the state of the brain if hubot was told to shutdown.  I tried to catch exit signals, however, hubot was doing the same.  When hubot caught an exit signal, it called process.exit().

There is no way to prevent the exiting of the event loop at this point, and once all 'exit' listeners have finished running the Node.js process will terminate. 8

In other words, if there are any asynchronous operations that started before process.exit() is called will not have a chance to complete.  I was convinced that I needed a synchronous way to write the state of the brain to etcd when process.exit() is called.  So, I added deasync and wrapped my write method with it, and called that function on process.exit().  Imagine something like the code below.

Much to my surprise, my deasynced function failed to write to the file in this case.  Even more perplexing, the console.log() that is called after it worked and printed the data to the command line.  If I couldn’t trap 'exit', maybe I could trap the same signal that prompted hubot to call process.exit(), 'SIGINT'Caution, the code below will not exit with a 'SIGINT', for example, when you press ctrl+c.  You will need to take more drastic measures like using the kill command to kill the process.

Although the process hung up, the data was written to the file, but the console.log() statement was never called.  On the surface, deasync sounds like a good solution to edge cases like this one.  Unfortunately, the behavior of deasync has unintended and unfixable 9 consequences.  For example, if you were using deasync to ensure graceful termination, you would actually be forced to ungracefully terminate the app in situations like the one I described above.

4.  It could cost you a significant refactor.

It seems that many are using deasync at all layers of their app instead of just the exit layer.  When I encountered problems with deasync, I researched the open issues for deasync and found that many users state that if they were forced to use an alternative to deasync, this would cause significant refactors, presumably because data at the top layer of their app comes from a “deasynced” function, or these functions are littered throughout the app.

This is a huge problem for us because changing it to async causes a cascade of refactors, which will ultimately cause nearly all of our sync functions to become async. 10

Of course many users reporting issues are maintainers of projects they inherited from others that were created at a time when the problems with deasync weren’t known.  However, the problems have now been known for years now.  Still, deasync sees 140,000 downloads a week.

5.  There are alternatives.

Before you consider deasync, consider the alternatives.  In my case, the use of process.exit was the source of my woes.  However, it is the hubot library and not my own extensions that call process.exit.  It would be better for hubot to allow the consumer to decide when to call process.exit rather than making the decision for them.

In the case of hubot dilemma, I could use one of the few synchronous methods available in the core library to store the state of hubot’s brain.  For example, I could use fs.writeFileSync to store the state to a json file.  The next time the app loads, I could tell it to read the state from the file.  Alternatively, I could use child_process.execSync to call an arbitrary shell command.  This could create security vulnerabilities arbitrary input is also accepted and it could also limit the portability of the app between operating systems.  To minimize portability issues, you could call your app from within itself via like the minimal example below shows.

In this example, if I run node index.js from the command line which binds an exit listener to process and then immediately calls process.exit.  The exit listener uses execSync to call the same script with an additional exit-save argument.  This makes an asynchronous http request and then writes the length of the response to stdout.  The response length is then written to stdout via console.log within the exit listener.  There are a number of limitations because data passed to stdout must be a string and data returned from execSync is also string (in the form of a buffer).  If you were to use this method to turn asynchronous functions into synchronous ones, you wouldn’t have access to JavaScript data structures, although I you could JSON encode and decode the data, or do some other form of string conversion, and then use eval to call any encoded functions.

All of this is obviously a hack and potentially could introduce security vulnerabilities if input is not sanitized, but it works, and is supported behavior as far as I am aware.  Still, the best alternative is to stick to using asynchronous control flow rather than devising hacks to introduce synchronous behavior to asynchronous functions in Node.js.  However, if you must do so, know that there are alternatives that don’t involve manipulating the runtime environment with native extensions.

I believe that there is place for synchronous methods in all I/O APIs for Node.js as there are clearly use cases.  The missing link is mainly network based I/O APIs, which offer no synchronous methods.  These could be useful for command line utilities where concurrency is not a significant issue, and users would be free to choose asynchronous methods when they needed to execute tasks concurrently.  Using synchronous network methods would clearly be a problem for applications like servers where many users will connect concurrently, but using synchronous file system and shell commands also problematic in this context.  If there are no technical limitations, the omission seems arbitrary.


  1. https://github.com/nodejs/node/pull/2830#issuecomment-207490532
  2. https://github.com/vkurchatkin/deasync
  3. https://github.com/abbr/deasync/issues/21
  4. https://github.com/nodejs/node/pull/2830#issuecomment-147784791
  5. https://github.com/nodejs/node/pull/2830#issuecomment-207483888
  6. https://github.com/nodejs/node/pull/2830#issuecomment-207490532
  7. https://github.com/abbr/deasync/issues/21
  8. https://nodejs.org/api/process.html#process_event_exit
  9. As far as the maintainers of Node.js are concered.
  10. https://github.com/nodejs/node/pull/2830#issuecomment-147527492