use node-redis with node 8 util.promisify

前端 未结 3 531
借酒劲吻你
借酒劲吻你 2021-02-05 02:19

node -v : 8.1.2

I use redis client node_redis with node 8 util.promisify , no blurbird.

the callback redis.get is ok, but promisify type get error message

3条回答
  •  我在风中等你
    2021-02-05 02:45

    You can also use Blue Bird Library plus monkey patching will do the trick for you. For example:

    const bluebird = require('bluebird')
    const redis = require('redis')
    async connectToRedis() {
         // use your url to connect to redis
         const url = '//localhost:6379'
         const client = await redis.createClient({
                    url: this.url
                }) 
         client.get = bluebird.promisify(client.get).bind(client);
         return client
       }
    // To connect to redis server and getting the key from redis
    connectToRedis().then(client => client.get(/* Your Key */)).then(console.log)
    

提交回复
热议问题