gpt4 book ai didi

javascript - Promisify Redis 客户端

转载 作者:IT王子 更新时间:2023-10-29 06:03:04 24 4
gpt4 key购买 nike

我如何promisify redis 以便我可以使用then

我已尝试向客户 promise :

var redis = require('redis');
Promise.promisifyAll(redis.RedisClient.prototype);
var client = redis.createClient();

client.on('connect', function(){
console.log('Redis connection is up');

client.lrange('abc',0,3).then(function(result){
console.log(result);
res.send(200)
});
});

但它返回错误:

client.lrange(...).then is not a function

PS:回调代码正常,说明服务器运行正常。

最佳答案

当使用 promisifyAll 时,promisified 方法得到一个 -Async 后缀:

client.lrangeAsync('abc',0,3).then(...);

根据 the documentation :

Note that the original methods on the object are not overwritten but new methods are created with the Async-suffix. For example, if you promisifyAll the node.js fs object use fs.statAsync to call the promisified stat method.

关于javascript - Promisify Redis 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39615384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com