Comparing version 1.1.0 to 1.1.1
@@ -29,7 +29,9 @@ module.exports = Pend; | ||
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) { | ||
@@ -36,0 +38,0 @@ pendGo(self, self.waiting.shift()); |
{ | ||
"name": "pend", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "dead-simple optimistic async helper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,2 +10,3 @@ # Pend | ||
var pend = new Pend(); | ||
pend.max = 10; // defaults to Infinity | ||
pend.go(function(cb) { | ||
@@ -12,0 +13,0 @@ console.log("this function is immediately executed"); |
11
test.js
@@ -57,4 +57,15 @@ var assert = require('assert'); | ||
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/); | ||
}); | ||
} |
3598
108
41