adonis-throttle icon indicating copy to clipboard operation
adonis-throttle copied to clipboard

Function returning a promise

Open kennedyrs opened this issue 5 years ago • 2 comments

When using Redis, the function below returns a promise to the count variable

count() {
        let count = this.store.get(this.key)
        if (typeof count === 'undefined') {
            return 0
        }
        return count
    }

kennedyrs avatar Jan 31 '20 14:01 kennedyrs

Hey @kennedyrs, thanks for reporting the bug. Redis driver uses promises because network I/O to Redis shouldn't block the Node.js event loop. This is not a problem for Memory driver since we can directly access the memory synchronously without blocking the event loop.

In fact, any driver written for external caches such as Redis, Memcached, etc. will have to be promises.

@masasron is it alright if I promisify all of the this.store methods? Memory driver will just resolve the promise right away (e.g. wrapped in Promise.resolve()) but it should make accessing the store much more consistent.

AndrewJo avatar Feb 04 '20 00:02 AndrewJo

@AndrewJo yes I think that would be a good idea, but since this is a breaking change for the memory driver we need to release this as a minor release.

masasron avatar Feb 06 '20 06:02 masasron