glynnbird closed pull request #130: Only return Promise from `headDoc` when no 
callback is specified.
URL: https://github.com/apache/couchdb-nano/pull/130
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/lib/nano.js b/lib/nano.js
index 572c9d9..2fa4408 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -504,25 +504,31 @@ module.exports = exports = function dbScope (cfg) {
 
     // 
http://docs.couchdb.org/en/latest/api/document/common.html#head--db-docid
     function headDoc (docName, callback) {
-      // this function doesn't pass on the Promise from relax because it needs
-      // to return the headers when resolving the Promise
-      return new Promise(function (resolve, reject) {
+      if (callback) {
         relax({
           db: dbName,
           doc: docName,
           method: 'HEAD',
           qs: {}
-        }, function (err, body, headers) {
-          if (callback) {
-            callback(err, body, headers)
-          }
-          if (err) {
-            reject(err)
-          } else {
-            resolve(headers)
-          }
-        })
-      })
+        }, callback);
+      } else {
+        // this function doesn't pass on the Promise from relax because it 
needs
+        // to return the headers when resolving the Promise
+        return new Promise(function (resolve, reject) {
+          relax({
+            db: dbName,
+            doc: docName,
+            method: 'HEAD',
+            qs: {}
+          }, function (err, body, headers) {
+            if (err) {
+              reject(err);
+            } else {
+              resolve(headers);
+            }
+          });
+        });
+      }
     }
 
     // 
http://docs.couchdb.org/en/latest/api/document/common.html#copy--db-docid
diff --git a/tests/unit/document/head.js b/tests/unit/document/head.js
new file mode 100644
index 0000000..9108b74
--- /dev/null
+++ b/tests/unit/document/head.js
@@ -0,0 +1,36 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy 
of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations 
under
+// the License.
+
+'use strict';
+
+const helpers = require('../../helpers/unit');
+const test = require('tape');
+const debug = require('debug')('nano/tests/unit/shared/error');
+
+const cli = helpers.mockClientDb(debug);
+const db = cli.use('foo');
+
+test('it should return a promise when no callback is specified', function 
(assert) {
+  var p = db.head('doc');
+  p.then((headers) => {
+    assert.equal(headers.statusCode, 200);
+    assert.end();
+  });
+});
+
+test('it should not return a promise when a callback is specified', function 
(assert) {
+  var p = db.head('doc', function(err, body, headers) {
+    assert.equal(headers.statusCode, 200);
+    assert.end();
+  });
+  assert.equal(typeof p, 'undefined');
+});


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to