Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
The deasync npm package allows you to convert asynchronous functions into synchronous ones. This can be useful in scenarios where you need to perform asynchronous operations in a synchronous manner, such as during initialization or configuration phases.
Convert Asynchronous Function to Synchronous
This feature allows you to convert an asynchronous function into a synchronous one. The code demonstrates how to use deasync to wait for an asynchronous function to complete before proceeding.
const deasync = require('deasync');
let done = false;
let result;
asyncFunction((err, res) => {
if (err) throw err;
result = res;
done = true;
});
while (!done) {
deasync.sleep(100);
}
console.log(result);
Sleep Function
The sleep function pauses the execution of code for a specified number of milliseconds. This can be useful for delaying operations or simulating long-running tasks.
const deasync = require('deasync');
console.log('Start');
deasync.sleep(2000);
console.log('End');
Synchronous Read File
This feature demonstrates how to read a file synchronously using deasync. The code waits for the asynchronous readFile operation to complete before proceeding.
const fs = require('fs');
const deasync = require('deasync');
let content;
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
content = data;
done = true;
});
while (!done) {
deasync.sleep(100);
}
console.log(content);
The sync-request package allows you to make synchronous HTTP requests. Unlike deasync, which can convert any asynchronous function to synchronous, sync-request is specifically designed for HTTP requests.
The wait-for-stuff package provides synchronous versions of common asynchronous operations like reading files and making HTTP requests. It offers a more limited scope compared to deasync, which can handle any asynchronous function.
The fibers package allows you to write asynchronous code in a synchronous style using fibers. It is more complex and powerful than deasync, offering more control over the execution flow but requiring a deeper understanding of fibers.
deasync provides a JavaScript wrapper to run Node.js event loop. The core of deasync is writen in C++.
Suppose you maintain a library that exposes a function getData
. Your users call it to get actual data:
var output = 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. For similar reason ES6 generators won't work either. What really needed is a way to block subsequent JavaScript from running without blocking entire thread. Ideally the blockage is removed as soon as the result of async function is available. A less ideal but acceptable alternative is a sleep
function which you can use to implement the blockage such as while(!done) sleep(100);
. It is less ideal because sleep duration has to be guessed. It is important the sleep
function doesn't block the thread, nor should it incur busy wait that pegs the CPU to 100%.
deasync provides an implementation of such sleep
function.
function SyncFunction(){
var ret;
setTimeout(function(){
ret = "hello";
},3000);
while(ret === undefined) {
require('deasync').sleep(100);
}
// returns hello with sleep and undefined otherwise
return ret;
}
var deasync = require('deasync');
var cp = require('child_process');
var exec = deasync(cp.exec);
// output result of ls -la
console.log(exec('ls -la'));
// done is printed last with deasync as anticipated and first otherwise.
console.log('done');
Prerequisites: deasync uses node-gyp to compile C++ source code so you need the compilers listed in in node-gyp.
To install, run
npm install deasync
FAQs
Turns async function into sync via JavaScript wrapper of Node event loop
The npm package deasync receives a total of 464,262 weekly downloads. As such, deasync popularity was classified as popular.
We found that deasync demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.