@fibjs/sync
v1.2.0
Published
Synchronize all kinds of async look like function to real non-blocking sync function, make the world peace and quiet!
Downloads
9
Readme
@fibjs/sync
Synchronize all kinds of async look like function to original non-blocking sync one, make the world peace and quiet!
Install
$ npm i @fibjs/sync --save
Usage
const sync = require('@fibjs/sync');
function cb(callback) {
callback(null, 'this is callback');
}
function pr() {
return new Promise((resovle, reject) => {
resovle('this is promise');
});
}
function* ge() {
return 'this is generator';
}
async function aa() {
return 'this is async function';
}
// sync default to callback hanlder
const callbackSync = sync(cb);
try {
const result = callbackSync();
} catch(e) {
// handle the error
}
// the other apis
const newCb = sync.cb(cb);// callback
const newPr = sync.pr(pr);// promise
const newGe = sync.ge(ge);// generator
const newAa = sync.aa(aa);// async function
Now you can use newCb
,newPr
,newGe
,newAa
as a normal sync function and use try/catch to handle the error.
Questions & Suggestions
Please open an issue here.