如何在节点js中使用异步和等待函数提取数据?

How to extract data using async and await function in node js?

我尝试使用 mongodb.I 在节点 js 中实现 redis 缓存 在 cache.but 中设置数据 我无法在 cache.how 中获取数据来解决这个问题。

cache.js

async function Get_Value(){
    let response = await client.get('products')
    console.log("_______________")
    console.log(response)

}

我得到输出:真

异常输出:json数据

如何使用缓存get方法获取json数据

Redis 不为 node.js 提供完整的异步等待适配器,因此通常作为解决方法,人们会承诺 lib。

const { promisify } = require('util');
const getAsync = promisify(client.get).bind(client);

async function getValue(){
    let response = await getAsync("products");
}

您可以使用另一种方法来 promisify 整个 redis 库:

const redis = require('redis');
bluebird.promisifyAll(redis);

现在您还可以使用使用 async/await 的方法。