deasynchronization

This commit is contained in:
Arik Sosman 2016-01-10 22:22:08 -08:00
parent b34571c800
commit d7dbe078dc
2 changed files with 14 additions and 6 deletions

View File

@ -2,14 +2,21 @@ if (typeof Buffer === 'undefined') {
global.Buffer = require('buffer').Buffer global.Buffer = require('buffer').Buffer
} }
var deasync = require('deasync');
var { var {
RNRandomBytes RNRandomBytes
} = require('react-native').NativeModules } = require('react-native').NativeModules;
export default function randomBytes (length, cb) { export default function randomBytes(length, cb) {
if (!cb) throw new Error('synchronous API not supported') 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) if (err) cb(err)
else cb(null, new Buffer(base64String, 'base64')) else cb(null, new Buffer(base64String, 'base64'))
}) })

View File

@ -14,7 +14,8 @@
"ios" "ios"
], ],
"dependencies": { "dependencies": {
"buffer": "^3.5.2" "buffer": "^3.5.2",
"deasync": "^0.1.4"
}, },
"author": "Mark Vayngrib <mark.vayngrib@lablz.com> (http://github.com/mvayngrib)", "author": "Mark Vayngrib <mark.vayngrib@lablz.com> (http://github.com/mvayngrib)",
"license": "ISC" "license": "ISC"