Skip to content

Commit 1ef099c

Browse files
LiranBridgeAR
authored andcommitted
Native Promises with util.promisify
explain how to promisify to native promises (instead of bluebird) with node's v8 util.promisify.
1 parent 0094795 commit 1ef099c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,27 @@ landscape.
5757

5858
### Promises
5959

60+
#### Native Promises
61+
If you are using node v8 or higher, you can promisify node_redis with [util.promisify](https://nodejs.org/api/util.html#util_util_promisify_original) as in:
62+
```js
63+
const {promisify} = require('util');
64+
const getAsync = promisify(client.get).bind(client);
65+
```
66+
now *getAsync* is a promisified version of *client.get*:
67+
```js
68+
// We expect a value 'foo': 'bar' to be present
69+
// So instead of writing client.get('foo', cb); you have to write:
70+
return getAsync('foo').then(function(res) {
71+
console.log(res); // => 'bar'
72+
});
73+
```
74+
75+
#### Bluebird Promises
6076
You can also use node_redis with promises by promisifying node_redis with
6177
[bluebird](https://github.com/petkaantonov/bluebird) as in:
6278

6379
```js
64-
var redis = require('redis');
80+
const redis = require('redis');
6581
bluebird.promisifyAll(redis.RedisClient.prototype);
6682
bluebird.promisifyAll(redis.Multi.prototype);
6783
```

0 commit comments

Comments
 (0)