use node-redis with node 8 util.promisify

前端 未结 3 532
借酒劲吻你
借酒劲吻你 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:55

    If you are using node v8 or higher, you can promisify node_redis with util.promisify as in:

    const {promisify} = require('util');
    const getAsync = promisify(client.get).bind(client); // now getAsync is a promisified version of client.get:
    
    // We expect a value 'foo': 'bar' to be present
    // So instead of writing client.get('foo', cb); you have to write:
    return getAsync('foo').then(function(res) {
        console.log(res); // => 'bar'
    });
    

    or using async await:

    async myFunc() {
        const res = await getAsync('foo');
        console.log(res);
    }
    

    culled shamelessly from redis official repo

提交回复
热议问题