Socket
Socket
Sign inDemoInstall

deasync

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deasync - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

4

package.json
{
"name": "deasync",
"version": "0.0.4",
"description": "JavaScript wrapper to Node.js event loop.",
"version": "0.0.5",
"description": "Turns async function into sync",
"main": "index.js",

@@ -6,0 +6,0 @@ "author": "Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>",

deasync
=======
deasync provides a JavaScript wrapper to run Node.js event loop. The core of deasync is writen in C++.
deasync turns async function into sync, implemented with a blocking mechanism by way of calling Node.js event loop at JavaScript layer. The core of deasync is writen in C++.

@@ -10,10 +10,24 @@ ## Motivation

You may tempted to use [node-fibers](https://github.com/laverdet/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%.
You may tempted to use [node-fibers](https://github.com/laverdet/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.
deasync provides an implementation of such `sleep` function.
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 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.
## Usages
* Sleep
* Generic wrapper of async function with standard API signature `function(p1,...pn,function cb(err,res){})`
```
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, as supposed, with cp.exec wrapped in deasync; first without.
console.log('done');
```
* Sleep (a wrapper of setTimeout)
```
function SyncFunction(){

@@ -27,3 +41,3 @@ var ret;

}
// returns hello with sleep and undefined otherwise
// returns hello with sleep; undefined without
return ret;

@@ -33,16 +47,4 @@ }

* Generic async wrapper
```
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');
```
## Installation
Prerequisites: deasync uses node-gyp to compile C++ source code so you need the compilers listed in in [node-gyp](https://github.com/TooTallNate/node-gyp).
Prerequisites: Except on a few platforms where binary distribution is included, deasync uses node-gyp to compile C++ source code so you may need the compilers listed in [node-gyp](https://github.com/TooTallNate/node-gyp).

@@ -49,0 +51,0 @@ To install, run

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc