@ivinokur/deasync
v0.2.2
Published
Turns async code into sync via JavaScript wrapper of Node event loop, support both callback and promise
Downloads
6
Maintainers
Readme
DeAsync
DeAsync turns async code into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. The core of deasync is written in C++.
This project is forked from abbr/deasync and rewritten in modern code. There are some new features added: TypeScript types, Promise support, and prebuild binaries.
Motivation
Suppose you maintain a library that exposes a function getData
. Your users call it to get actual data:const myData = getData()
Under the hood data is saved in a file so you implemented getData
using Node.js built-in fs.readFileSync
. It's obvious both getData
and fs.readFileSync
are sync functions. One day you were told to switch the underlying data source to a repo such as MongoDB which can only be accessed asynchronously. You were also told to avoid pissing off your users, getData
API cannot be changed to return merely a promise or demand a callback parameter. How do you meet both requirements?
You may tempted to use node-fibers or a module derived from it, but node fibers can only wrap async function call into a sync function inside a fiber. In the case above you cannot assume all callers are inside fibers. On the other hand, if you start a fiber in getData
then getData
itself will still return immediately without waiting for the async call result. For similar reason ES6 generators introduced in Node v0.11 won't work either.
What really needed is a way to block subsequent JavaScript from running without blocking entire thread by yielding to allow other events in the event loop to be handled. Ideally the blockage is removed as soon as the result of async function is available. A less ideal but often acceptable alternative is a sleep
function which you can use to implement the blockage like while(!done) sleep(100);
. It is less ideal because sleep duration has to be guessed. It is important the sleep
function not only shouldn't block entire thread, but also shouldn't incur busy wait that pegs the CPU to 100%.
DeAsync supports both alternatives.
Installation
npm install @kaciras/deasync
DeAsync downloads prebuild binary from GitHub releases during installation, if the download fails, try to build locally. You can skip the installation phase by set environment variable NO_PREBUILD=1
.
DeAsync uses node-gyp to compile C++ source code, so to build Deasync you may need the compilers listed in node-gyp.
Usage
Deasync exports two APIs: deasync
for callback style function, and awaitSync
for Promise.
deasync(function)
Generic wrapper of async function with conventional API signature function(...args, (error, result) => {})
. Returns result
and throws error
as exception if not null.
Sleep (a wrapper of setTimeout):
const { deasync } = require("@kaciras/deasync");
const sleep = deasync((timeout, done) => {
setTimeout(() => done(null, "wake up!"), timeout);
});
console.log("Timestamp before: " + performance.now());
console.log(sleep(1000));
console.log("Timestamp after: " + performance.now());
awaitSync(promise)
The awaitSync
causes execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of after fulfillment. When resumed, the returned value of the awaitSync
is that of the fulfilled Promise. If the Promise is rejected, the awaitSync
throws the rejected value.
This function is similar with the keyword await
but synchronously.
const { awaitSync } = require("@kaciras/deasync");
const { performance } = require("perf_hooks");
console.log("Timestamp before: " + performance.now());
const promise = new Promise(resolve => setTimeout(resolve, 1000)).then(() => "wake up!")
console.log(awaitSync(promise));
console.log("Timestamp after: " + performance.now());
Recommendation
Unlike other (a)sync js packages that mostly have only syntactic impact, DeAsync also changes code execution sequence. As such, it is intended to solve niche cases like the above one. If all you are facing is syntatic problem such as callback hell, using a less drastic package implemented in pure js is recommended.