-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Promise #864
Comments
I don't believe that is on the roadmap. You can, however, promisify the library with bluebird. There is even a specific example for this library. |
I do like to add a way to promisify the library if a certain parameter is passed. This won't be in the next release though. |
@BridgeAR Interesting. If the option parameter is passed will it essentially just use bluebird? Or will it be implemented in a different way? |
Yes, I'd use bluebird internal. |
Related project: |
As for me, it would be convenient to return a Promise if the last argument isn't a callback function. I implemented it in my redis client. Refer to https://github.com/luin/ioredis for more details. Here's an example: redis.get('foo', function (err, result) {
console.log(result);
});
// Or using a promise if the last argument isn't a function
redis.get('foo').then(function (result) {
console.log(result);
}); |
That might be an option. I'll think about it when I have the time to tackle this. |
Probably it could be better for performance to return "thenable" instead of promise - no need to create promise wrapper for each item of chained commands. Wrapping will happen only on |
While it would require a lot more work, I'd recommend using native promises instead of a library like bluebird. As much as I like bluebird, using it will increase the number of dependencies and isn't the only library used. Mongoose uses native promises but allows replacing it with any promise implementation by the user, which is convenient for everyone. See http://mongoosejs.com/docs/promises.html#plugging-in-your-own-promises-library |
Hi, just wondering if this is being worked on. Definitely one of the most requested features. |
With then-redis no longer maintained this issue becomes really critical. Adding bluebird to runtime which supports promises natively doesn't make any sense — so people end up inventing promisifying wrappers 😭 |
I would be willing to PR this if the maintainers are interested. |
IMO it'd be very good if said promises handled errors in the standard promise way instead with 'on' handlers. (excuse my coffeescript):
I really want to handle redis operation errors in the code where I'm actually doing the operation. ioredis seems to get that very wrong in its promise "implementation". |
Hi @jayjanssen, ioredis implements Promise in a standard way (by means of bluebird). For instance: redis.get('foo', 'bar').then(console.log).catch(err => {
// got an error since `GET` only accepts one parameter.
}) I'm wondering in which case do you need the 'on' handlers. |
I was just testing this today and I would swear I had errors getting thrown all over the place and not being properly handled in the .catch. I'd be happy to try it again. |
@luin see redis/ioredis#433 , thanks. |
For all people that do not want to use the bluebird lib: import { promisifyAll, } from 'sb-promisify'
import redis from 'redis'
const RedisClient = redis.RedisClient.prototype
const Multi = redis.Multi.prototype
const RedisClientP = promisifyAll(redis.RedisClient.prototype)
const MultiP = promisifyAll(redis.Multi.prototype)
redis.RedisClient.prototype = Object.assign(RedisClient, RedisClientP)
redis.Multi.prototype = Object.assign(Multi, MultiP) |
The then-redis repository claims that it is not natively supported. Is that the case ? If not that then, can I wrap this with node 8.0.0 feature util.promisify merged here |
Does it support promise natively, or I still have to use bluebird? |
@kahwooi You do not need to use bluebird, you could use the method I posted. If you are using the newest node version I'm pretty sure you could also monkey patch the objects with util.promisify (http://2ality.com/2017/05/util-promisify.html) |
Can you detail what is "if the last argument isn't a callback function"? Looks like, last argument is a function in get():
which document are you referring to when you mention last argument? |
@manoharreddyporeddy it means that if you don't provide the last argument to the |
Makes a lot of sense now. "if the last argument isn't a callback function" Above wording, or similar, is easy to understand, and is more correct. Thank you |
+1 to have native promise support without external libs into this! And +1 to have as the nowadays standard: if no callback is provided then return a promise :) |
Version check.
You're right, it's not exactly a hack.. it is mutating the library though, which always makes me a bit uneasy. |
@LoganDark How about this: const util = require('util')
const redis = require('redis')
const client = redis.createClient()
const handler = {
get(target, propKey, receiver) {
return util.promisify(target[propKey]).bind(target)
},
}
const clientProxy = new Proxy(client, handler) No mutation of the core library 😉 Beside that: I would love to see |
@n1ru4l that's a biased approach when working with js ecosystem.
I agree but this is a good thing imho. If the best implementation requires an up-to-date nodejs then we can have developers choose:
Sooner or later ioredis (as every software) will reach the line when developers have to update their ecosystems to use it because of features we will have on node: why waiting then? 🤔 |
@stockholmux Are you open for a pull request that makes the methods return a native |
In case anyone is interested. I went ahead and created promisify-redis for myself, I will us this from now on. |
@stockholmux The approach @n1ru4l suggests is great as it prevents breaking changes on existing code while still allowing to use promises. This is also the approach the official node mongodb lib has taken to support promises. |
@jmparsons holy fuck that would be horrible. |
@jmparsons are you claiming there were promises early on? :P |
Take look at ioredis, luckily it has compatibility with native promises |
Majority of people want Promises, majority of people are willing to contribute to make that happen. Opening a PR that pushes that idea forward would be wise idea, I think. |
Even if I promisify this library, I'll lose typescript typings. If you want to use typescript and promises node_redis is essentially a no-go. |
This doesn't work:
nor does this
Is this really the only way to do this?
|
I want native promises with this module, too. However—until it ships—what about this? Just promisifying and using const util = require('util');
const redis = require('redis');
const client = redis.createClient();
const clientAsync = util.promisify(client.sendCommand).bind(client);
(async function () => {
console.log(await clientAsync('set', ['best-pizza', 'never pineapple'])); // OK
console.log(await clientAsync('get', ['best-pizza'])); // never pineapple
console.log(await clientAsync('del', ['best-pizza'])); // 1
clientAsync('quit');
})(); Is there a technical problem with this approach? |
@ohjames just came across the handy-redis project https://github.com/mmkal/handy-redis Haven't tested it myself but might be worth a try... |
AFAIK that method doesn't appear to work with |
+1 |
@anasanzari Just don't |
I would like to request that if native promise support is implemented in the future that callbacks are not removed. We specifically look for libraries that support callbacks and not promises to reduce memory allocations. (And by the way, if callbacks are implemented on top of promises by the library for backwards compatibility then that would defeat the purpose, this would be pointless as internally the library would be doing all the allocations related to promises). Just to leave my two cents out there that the callback design is sometimes preferred. |
if you're worrying about memory allocations, why use high level languages? If you care about low memory usage, then go use lower level langs. |
Will this project support Promise (which has already supported in Node 4.x)?
The text was updated successfully, but these errors were encountered: