Emulating bluebird.promisifyAll with util.promisify Emulating bluebird.promisifyAll with util.promisify typescript typescript

Emulating bluebird.promisifyAll with util.promisify


Came across this as I was looking for a same thing. This is what I'm using currently and it seems to work:

const { promisify } = require('util')const redis = require('redis')const client = redis.createClient('redis://localhost:6379')client.promise = Object.entries(redis.RedisClient.prototype)  .filter(([_, value]) => typeof value === 'function')  .reduce((acc, [key, value]) => ({    ...acc,    [key]: promisify(value).bind(client)  }), {})client.on('connect', async () => {  await client.promise.set('foo', 'bar')  console.log(await client.promise.get('foo'))})


You could use fluentify, it's tiny and has no external dependencies and pretty much does what your after. I've actually used it myself for Redis, there are some small caveats but in your case should be fairly straightforward e.g.

import * as _redis from 'redis';import fluentify from 'fluentifyjs';...const client = fluentify(_redis.createClient());const result = await client.get('key').done();

I get that your probably looking to do this natively, however, thought it'd be worth mentioning as an alternative, considering you'd get method chaining for free if needed.

Disclaimer - I'm the author


I have tried the following module, it's very simple to use and can be used to replace bluebird promises. link

https://www.npmjs.com/package/util-promisifyall