Turns async function into sync via JavaScript wrapper of Node event loop
npm install deasyncDeAsync.js
=======

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++.
getData. Your users call it to get actual data:
var myData = getData();
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?
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.
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
* Generic wrapper of async function with conventional API signature function(p1,...pn,function cb(error,result){}). Returns result and throws error as exception if not null:
`javascript
var deasync = require('deasync');
var cp = require('child_process');
var exec = deasync(cp.exec);
// output result of ls -la
try{
console.log(exec('ls -la'));
}
catch(err){
console.log(err);
}
// done is printed last, as supposed, with cp.exec wrapped in deasync; first without.
console.log('done');
`
* For async function with unconventional API, for instance function asyncFunction(p1,function cb(res){}), use loopWhile(predicateFunc) where predicateFunc is a function that returns boolean loop condition
`javascript
var done = false;
var data;
asyncFunction(p1,function cb(res){
data = res;
done = true;
});
require('deasync').loopWhile(function(){return !done;});
// data is now populated
`
* Sleep (a wrapper of setTimeout)
`javascript
function SyncFunction(){
var ret;
setTimeout(function(){
ret = "hello";
},3000);
while(ret === undefined) {
require('deasync').sleep(100);
}
// returns hello with sleep; undefined without
return ret;
}
`
Installation
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`
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 syntactic problem such as callback hell, using a less drastic package implemented in pure js is recommended.
Support
Pull requests and issue reporting are welcome. For issues to be considered by maintainer
1. they must be reproducible
2. there must be evidence the issue is related to DeAsync
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.