【发布时间】:2020-08-15 02:38:35
【问题描述】:
我正在编写一个函数来使用 crypto.randomBytes 生成一个随机密钥,它需要一个回调。我更喜欢使用异步等待,所以我尝试使用 util.promisify 来包装随机字节,如下所示:
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);
但这会打印 key: Promise { <pending> } 而不是打印解析的值。我在这里做错了什么?
【问题讨论】:
标签: node.js async-await node-promisify