[Pkg-javascript-commits] [node-pend] 05/10: perfect detection of calling callback twice

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:11:00 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-pend.

commit 1a1826063f4acf162b00ded0b2a1e2c1c7165734
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Tue Sep 24 03:30:28 2013 -0400

    perfect detection of calling callback twice
---
 index.js |  4 +++-
 test.js  | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/index.js b/index.js
index ae64857..317993b 100644
--- a/index.js
+++ b/index.js
@@ -26,11 +26,13 @@ Pend.prototype.wait = function(cb) {
 
 function pendGo(self, fn) {
   self.pending += 1;
+  var called = false;
   fn(onCb);
   function onCb(err) {
+    if (called) throw new Error("callback called twice");
+    called = true;
     self.error = self.error || err;
     self.pending -= 1;
-    if (self.pending < 0) throw new Error("Callback called twice.");
     if (self.waiting.length > 0 && self.pending < self.max) {
       pendGo(self, self.waiting.shift());
     } else if (self.pending === 0) {
diff --git a/test.js b/test.js
index e744281..7f5f5a0 100644
--- a/test.js
+++ b/test.js
@@ -54,6 +54,17 @@ function testWithMax() {
   });
   pend.wait(function(err) {
     assert.deepEqual(results, ['a', 'b', 1, 'c', 1, 2]);
+    testCallbackTwice();
   });
   assert.deepEqual(results, ['a', 'b']);
 }
+
+function testCallbackTwice() {
+  var pend = new Pend();
+  pend.go(function(cb) {
+  });
+  pend.go(function(cb) {
+    cb();
+    assert.throws(cb, /callback called twice/);
+  });
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-pend.git



More information about the Pkg-javascript-commits mailing list