deasynchronization
This commit is contained in:
parent
b34571c800
commit
d7dbe078dc
17
index.js
17
index.js
|
@ -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'))
|
||||||
})
|
})
|
||||||
|
|
|
@ -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"
|
||||||
|
|
Loading…
Reference in New Issue