@fibjs/sync

1.2.0 • Public • Published

@fibjs/sync

NPM version build status appveyor build status Test coverage David deps Known Vulnerabilities npm download

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.

License

MIT

Package Sidebar

Install

npm i @fibjs/sync

Weekly Downloads

1

Version

1.2.0

License

MIT

Last publish

Collaborators

  • ngot-fibjs
  • ngot