【问题标题】:How to use async/await using crypto.randomBytes in nodejs?如何在nodejs中使用crypto.randomBytes来使用异步/等待?
【发布时间】:2021-07-30 10:23:54
【问题描述】:
const crypto = require('crypto');

async function getKey(byteSize) {
    let key = await crypto.randomBytes(byteSize);
    return key;
}

async function g() {
    let key = await getKey(12);
    return key;
}

console.log(g());

console.log('hello - want this called after g() above');

我已经研究了一个小时,但我不明白如何确保使用 async/await 获得密钥。无论我做什么,我都会得到一个 Promise-pending。

我也试过这个:

async function getKey(byteSize) {
    let key = await crypto.randomBytes(byteSize);
    return key;
}

getKey(12).then((result) => { console.log(result) })


console.log('hello');

...无济于事!灵感来自: How to use await with promisify for crypto.randomBytes?

谁能帮我解决这个问题?

我要做的就是让 randomBytes 异步。使用 async./await 块,但在我继续代码之前确保它履行了承诺。

【问题讨论】:

  • 由于您没有承诺或将回调传递给crypto.randomBytes(),因此它是同步的,因此您无法等待它。此外,您没有正确等待顶级 g() 返回的承诺。这就是为什么您总是在 console.log() 中看到待处理的 Promise
  • 有简单的例子吗?
  • 我提供了一个答案来证明我的评论。

标签: javascript node.js


【解决方案1】:

这是我对该问题的评论的延伸

由于您没有承诺或将回调传递给crypto.randomBytes(),因此它是同步的,因此您无法等待它。此外,您没有正确等待顶级 g() 返回的承诺。这就是为什么您总是在 console.log() 中看到待处理的 Promise

您可以使用util.promisify()crypto.randomBytes() 转换为promise 返回函数并等待它。在您的示例中不需要async/await,因为所做的只是用承诺包装承诺。

const { promisify } = require('util')
const randomBytesAsync = promisify(require('crypto').randomBytes)

function getKey (size) { 
  return randomBytesAsync(size)
}

// This will print the Buffer returned from crypto.randomBytes()
getKey(16)
  .then(key => console.log(key))

如果您想在 async/await 样式函数中使用 getKey(),则可以这样使用

async function doSomethingWithKey () {
  let result
  const key = await getKey(16)
   
  // do something with key

  return result
}

【讨论】:

    【解决方案2】:
    const crypto = require("crypto");
    
    async function getRandomBytes(byteSize) {
      return await new Promise((resolve, reject) => {
        crypto.randomBytes(byteSize, (err, buffer) => {
          if (err) {
            reject(-1);
          }
          resolve(buffer);
        });
      });
    }
    
    async function doSomethingWithRandomBytes(byteSize) {
      if (!byteSize) return -1;
      const key = await getRandomBytes(byteSize);
      //do something with key
    }
    
    doSomethingWithRandomBytes(16);
    

    【讨论】:

      【解决方案3】:
        const crypto = require('crypto');
          
          async function getKey(byteSize) {
              const buffer = await new Promise((resolve, reject) => {
          crypto.randomBytes(byteSize, function(ex, buffer) {
            if (ex) {
              reject("error generating token");
            }
            resolve(buffer);
          });
          }
          
          async function g() {
              let key = await getKey(12);
              return key;
          }
      

      【讨论】:

        猜你喜欢
        • 2020-07-11
        • 1970-01-01
        • 1970-01-01
        • 2021-08-27
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多