From d7dbe078dc1078a7b18692966a1383b1e21d3a4b Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Sun, 10 Jan 2016 22:22:08 -0800 Subject: [PATCH] deasynchronization --- index.js | 17 ++++++++++++----- package.json | 3 ++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index bb08527..61ac663 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,21 @@ if (typeof Buffer === 'undefined') { global.Buffer = require('buffer').Buffer } +var deasync = require('deasync'); + var { - RNRandomBytes -} = require('react-native').NativeModules +RNRandomBytes +} = require('react-native').NativeModules; -export default function randomBytes (length, cb) { - if (!cb) throw new Error('synchronous API not supported') +export default function randomBytes(length, cb) { + if (!cb) { + // throw new Error('synchronous API not supported') + var exec = deasync(RNRandomBytes.randomBytes); + var base64String = exec(length); + return new Buffer(base64String, 'base64'); + } - RNRandomBytes.randomBytes(length, function (err, base64String) { + RNRandomBytes.randomBytes(length, function(err, base64String) { if (err) cb(err) else cb(null, new Buffer(base64String, 'base64')) }) diff --git a/package.json b/package.json index 888bcdc..e4ac61e 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "ios" ], "dependencies": { - "buffer": "^3.5.2" + "buffer": "^3.5.2", + "deasync": "^0.1.4" }, "author": "Mark Vayngrib (http://github.com/mvayngrib)", "license": "ISC"