Synchron
Wrapping an async function into a synchron function call without blocking.
Install
npm install synchron --save
Using Synchron
Create a new instance of Synchro and use the methods return
and throw
. The wait
method will wait until
return
or throw
was called.
const { Synchron } = require('synchron');
var asyncTimeout = new Synchron();
setTimeout(function(){
console.log('inside setTimeout');
asyncTimeout.done();
}, 500);
asyncTimeout.wait();
console.log('back in main');
Create a new instance and pass a function to wrap the asnyc call into a synchron function.
The warpped function will run in the context of Synchron
, so you can use this.return
and/or this.throw
.
If you like to exit the async function without returning a result just call this.return()
without a parameter or call this.done()
.
const { Synchron } = require('synchron');
var readFileSync = new Synchron(function(filename) {
var fs = require('fs');
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
this.throw(err);
} else {
this.return(data);
}
});
});
try {
var data = readFileSync('./testfile.txt');
console.log(data);
} catch (err){
console.log(err);
}