Skip to content

Instantly share code, notes, and snippets.

@akirattii
Last active July 6, 2017 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirattii/edf9d397bea06941959f3b52013e9a86 to your computer and use it in GitHub Desktop.
Save akirattii/edf9d397bea06941959f3b52013e9a86 to your computer and use it in GitHub Desktop.
How to use an excellent module "deasync" which makes async function to sync function easily.
/**
NOTE: deasync can not use on browser.
*/
var deasync = require('deasync');
// any async function error-first callback styled
function asyncFn(p, cb) {
let res = "hello " + p;
let err = null;
return cb && cb(err, res);
}
/** Use as async */
asyncFn("async world", (err, res) => {
console.log("asyncFn callback:", res);
});
/** Use as sync! */
let syncFn = deasync(asyncFn);
let result = syncFn("sync world");
console.log("syncFn result:", result);
/*
Result:
asyncFn callback: hello async world
syncFn result: hello sync world
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment