summaryrefslogtreecommitdiff
path: root/std/node/_crypto/randomBytes.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/node/_crypto/randomBytes.ts')
-rw-r--r--std/node/_crypto/randomBytes.ts12
1 files changed, 10 insertions, 2 deletions
diff --git a/std/node/_crypto/randomBytes.ts b/std/node/_crypto/randomBytes.ts
index 28c7e454e..335c92d68 100644
--- a/std/node/_crypto/randomBytes.ts
+++ b/std/node/_crypto/randomBytes.ts
@@ -39,8 +39,9 @@ export default function randomBytes(
cb?: (err: Error | null, buf?: Buffer) => void,
): Buffer | void {
if (typeof cb === "function") {
+ let err: Error | null = null, bytes: Buffer;
try {
- cb(null, generateRandomBytes(size));
+ bytes = generateRandomBytes(size);
} catch (e) {
//NodeJS nonsense
//If the size is out of range it will throw sync, otherwise throw async
@@ -50,9 +51,16 @@ export default function randomBytes(
) {
throw e;
} else {
- cb(e);
+ err = e;
}
}
+ setTimeout(() => {
+ if (err) {
+ cb(err);
+ } else {
+ cb(null, bytes);
+ }
+ }, 0);
} else {
return generateRandomBytes(size);
}