Last active
July 6, 2017 06:35
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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