From 741f52810d5005315ff062203558c18c20a5ec30 Mon Sep 17 00:00:00 2001 From: "Chris West (Faux)" Date: Wed, 8 Apr 2020 17:11:47 +0100 Subject: [PATCH] chore: nodeify is a free function --- redlock.js | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/redlock.js b/redlock.js index 5ec0285..51f074e 100644 --- a/redlock.js +++ b/redlock.js @@ -62,14 +62,6 @@ const defaults = { }; -// MONKEYPATCH -Promise.prototype.nodeify = function (callback) { - if (typeof callback === 'function') { - this.then((v) => callback(undefined, v)) - .catch((err) => callback(err)); - } - return this; -}; @@ -160,12 +152,12 @@ Redlock.LockError = LockError; Redlock.prototype.quit = function quit(callback) { // quit all clients - return Promise.all(this.servers.map(function(client) { + return nodeify(Promise.all(this.servers.map(function(client) { return client.quit(); - })) + })), // optionally run callback - .nodeify(callback); + callback); }; @@ -224,7 +216,7 @@ Redlock.prototype.unlock = function unlock(lock, callback) { // immediately invalidate the lock lock.expiration = 0; - return new Promise(function(resolve, reject) { + return nodeify(new Promise(function(resolve, reject) { // the number of votes needed for consensus const quorum = Math.floor(self.servers.length / 2) + 1; @@ -265,10 +257,10 @@ Redlock.prototype.unlock = function unlock(lock, callback) { // FAILURE: the lock could not be released return reject(new LockError('Unable to fully release the lock on resource "' + lock.resource + '".')); } - }) + }), // optionally run callback - .nodeify(callback); + callback); }; @@ -280,20 +272,20 @@ Redlock.prototype.extend = function extend(lock, ttl, callback) { // the lock has expired if(lock.expiration < Date.now()) - return Promise.reject(new LockError('Cannot extend lock on resource "' + lock.resource + '" because the lock has already expired.', 0)).nodeify(callback); + return nodeify(Promise.reject(new LockError('Cannot extend lock on resource "' + lock.resource + '" because the lock has already expired.', 0)), callback); // extend the lock - return self._lock(lock.resource, lock.value, ttl, {}) + return nodeify(self._lock(lock.resource, lock.value, ttl, {}) // modify and return the original lock object .then(function(extension){ lock.value = extension.value; lock.expiration = extension.expiration; return lock; - }) + }), // optionally run callback - .nodeify(callback); + callback); }; @@ -340,7 +332,7 @@ Redlock.prototype._lock = function _lock(resource, value, ttl, options, callback // array of locked resources resource = Array.isArray(resource) ? resource : [resource]; - return new Promise(function(resolve, reject) { + return nodeify(new Promise(function(resolve, reject) { let request; // the number of times we have attempted this lock @@ -422,10 +414,10 @@ Redlock.prototype._lock = function _lock(resource, value, ttl, options, callback } return attempt(); - }) + }), // optionally run callback - .nodeify(callback); + callback); }; @@ -451,4 +443,12 @@ Redlock.prototype._hashScript = function(value) { return crypto.createHash('sha1').update(value).digest('hex'); } +function nodeify(promise, callback) { + if (typeof callback === 'function') { + promise.then((v) => callback(undefined, v)) + .catch((err) => callback(err)); + } + return promise; +} + module.exports = Redlock;