Gathering detailed insights and metrics for deasync
Gathering detailed insights and metrics for deasync
Gathering detailed insights and metrics for deasync
Gathering detailed insights and metrics for deasync
synckit
Perform async work synchronously in Node.js using `worker_threads` with first-class TypeScript support.
@types/deasync
TypeScript definitions for deasync
deasync-promise
Transform async functions into sync with promise API
@kaciras/deasync
Turns async code into sync via JavaScript wrapper of Node event loop, support both callback and promise
Turns async function into sync via JavaScript wrapper of Node event loop
npm install deasync
Typescript
Module System
Min. Node Version
Node Version
NPM Version
61.2
Supply Chain
99.5
Quality
75.7
Maintenance
100
Vulnerability
100
License
JavaScript (81.49%)
Python (11.48%)
C++ (7.03%)
Total Downloads
224,882,916
Last Day
31,567
Last Week
515,544
Last Month
2,223,484
Last Year
25,324,998
MIT License
993 Stars
118 Commits
73 Forks
23 Watchers
1 Branches
1 Contributors
Updated on Jun 12, 2025
Minified
Minified + Gzipped
Latest Version
0.1.30
Package Id
deasync@0.1.30
Unpacked Size
7.04 MB
Size
2.69 MB
File Count
110
NPM Version
10.7.0
Node Version
22.2.0
Published on
Jun 02, 2024
Cumulative downloads
Total Downloads
Last Day
-22.5%
31,567
Compared to previous day
Last Week
-11.6%
515,544
Compared to previous week
Last Month
4.7%
2,223,484
Compared to previous month
Last Year
-30.6%
25,324,998
Compared to previous year
2
1
DeAsync turns async function into sync, implemented with a blocking mechanism by calling Node.js event loop at JavaScript layer. The core of deasync is written in C++.
Suppose you maintain a library that exposes a function getData
. Your users call it to get actual data:
var 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 for backward compatibility, 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.
function(p1,...pn,function cb(error,result){})
. Returns result
and throws error
as exception if not null:1var deasync = require('deasync'); 2var cp = require('child_process'); 3var exec = deasync(cp.exec); 4// output result of ls -la 5try{ 6 console.log(exec('ls -la')); 7} 8catch(err){ 9 console.log(err); 10} 11// done is printed last, as supposed, with cp.exec wrapped in deasync; first without. 12console.log('done');
function asyncFunction(p1,function cb(res){})
, use loopWhile(predicateFunc)
where predicateFunc
is a function that returns boolean loop condition1var done = false; 2var data; 3asyncFunction(p1,function cb(res){ 4 data = res; 5 done = true; 6}); 7require('deasync').loopWhile(function(){return !done;}); 8// data is now populated
1function SyncFunction(){ 2 var ret; 3 setTimeout(function(){ 4 ret = "hello"; 5 },3000); 6 while(ret === undefined) { 7 require('deasync').sleep(100); 8 } 9 // returns hello with sleep; undefined without 10 return ret; 11}
Except on a few platforms + Node version combinations where binary distribution is included, DeAsync uses node-gyp to compile C++ source code so you may need the compilers listed in node-gyp. You may also need to update npm's bundled node-gyp.
To install, run
npm install deasync
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 syntactic problem such as callback hell, using a less drastic package implemented in pure js is recommended.
Pull requests and issue reporting are welcome. For issues to be considered by maintainer
To that end, the issue should contain platform information, error message relevant to DeAsync, and preferably code snippet. If code snippet is supplied, it must be self-contained, i.e. independent from your runtime environment or other modules not explicitly specified via require
in the code snippet.
The MIT License (MIT)
Copyright (c) 2015
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 7/27 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More