How to use await with promisify for crypto.randomBytes?
问题 I'm writing a function to generate a random key using crypto.randomBytes, which takes a callback. I'd prefer to use async await so I'm trying to use util.promisify to wraprandom bytes like this: const crypto = require('crypto'); const util = require('util'); const randBytes = util.promisify(crypto.randomBytes); async function genRandKey() { bytes = await randBytes(48).catch((err) => { console.log(err); }); return bytes.toString('hex'); } let result = genRandKey(); console.log('key: ', result)