https://github.com/planetarydev/synchron
https://github.com/planetarydev/synchron
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/planetarydev/synchron
- Owner: planetarydev
- Created: 2017-09-04T05:13:38.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-04T09:28:06.000Z (almost 8 years ago)
- Last Synced: 2025-03-18T14:46:14.549Z (3 months ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Synchron
Wrapping an async function into a synchron function call **without blocking**.
## Install
```shell
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.```javascript
const { Synchron } = require('synchron');var asyncTimeout = new Synchron();
setTimeout(function(){
// do something
console.log('inside setTimeout');
// and than call done
asyncTimeout.done();
}, 500);// non-blocking wait until done was called
asyncTimeout.wait();
console.log('back in main');// output:
// -------
// inside setTimeout
// 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()`.```javascript
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 {
// here is the synchron function call
var data = readFileSync('./testfile.txt');
console.log(data);
} catch (err){
// if an error occours the function will throw it and you can catch it
console.log(err); // maybe an error like: "ENOENT: no such file or directory, open './testfile.txt'"
}
```