Comparing version 1.1.0 to 2.0.0
13
index.js
@@ -0,1 +1,3 @@ | ||
var once = require('once'); | ||
var isError = function(e) { | ||
@@ -7,3 +9,3 @@ return Object.prototype.toString.call(e) === '[object Error]' || e instanceof Error; | ||
afterAllCb = afterAllCb || function() {}; | ||
afterAllCb = once(afterAllCb || function() {}); | ||
@@ -14,2 +16,3 @@ var errorMessage ='"next" function called after the final callback.'+ | ||
var done = false; | ||
var finalError = null; | ||
@@ -28,8 +31,4 @@ process.nextTick(function() { | ||
return function thecallback(err) { | ||
if (done) return; | ||
if (isError(err)) { | ||
done = true; | ||
return afterAllCb(err); | ||
} | ||
var args = arguments; | ||
if (isError(err) && !finalError) finalError = err; | ||
process.nextTick(function() { | ||
@@ -39,3 +38,3 @@ if (cb) cb.apply(null, args); | ||
done = true; | ||
afterAllCb(); | ||
afterAllCb(finalError); | ||
} | ||
@@ -42,0 +41,0 @@ }); |
{ | ||
"name": "after-all", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Execute several async functions and get a callback when they are all done", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "node_modules/mocha/bin/mocha" | ||
"test": "tape test" | ||
}, | ||
@@ -17,5 +17,7 @@ "author": "Eduardo Sorribas <eduardo@sorribas.org> (http://sorribas.org/)", | ||
"devDependencies": { | ||
"mocha": "1.17.1", | ||
"should": "3.1.2" | ||
"tape": "^2.13.3" | ||
}, | ||
"dependencies": { | ||
"once": "^1.3.0" | ||
} | ||
} |
@@ -0,141 +1,161 @@ | ||
var test = require('tape'); | ||
var afterAll = require('../index'); | ||
require('should'); | ||
describe('after-all', function() { | ||
it('should call the callback "after all" the functions are done', function(done) { | ||
var a,b,c,d; | ||
var next = afterAll(function() { | ||
a.should.eql(2); | ||
b.should.eql(4); | ||
c.should.eql(6); | ||
d.should.eql(8); | ||
done(); | ||
}); | ||
test('should call the callback "after all" the functions are done', function(t) { | ||
var a,b,c,d; | ||
var next = afterAll(function() { | ||
t.equal(a, 2); | ||
t.equal(b, 4); | ||
t.equal(c, 6); | ||
t.equal(d, 8); | ||
t.end(); | ||
}); | ||
setTimeout(next(function() { | ||
a = 2; | ||
}, 400)); | ||
setTimeout(next(function() { | ||
a = 2; | ||
}, 400)); | ||
setTimeout(next(function() { | ||
b = 4; | ||
}, 100)); | ||
setTimeout(next(function() { | ||
b = 4; | ||
}, 100)); | ||
setTimeout(next(function() { | ||
c = 6; | ||
}, 300)); | ||
setTimeout(next(function() { | ||
c = 6; | ||
}, 300)); | ||
setTimeout(next(function() { | ||
d = 8; | ||
}, 200)); | ||
setTimeout(next(function() { | ||
d = 8; | ||
}, 200)); | ||
}); | ||
test('should work with non-asynchronous functions', function(t) { | ||
var a,b,c,d; | ||
var next = afterAll(function() { | ||
t.equal(a, 2); | ||
t.equal(b, 4); | ||
t.equal(c, 6); | ||
t.equal(d, 8); | ||
t.end(); | ||
}); | ||
it('should work with non-asynchronous functions', function(done) { | ||
var a,b,c,d; | ||
var next = afterAll(function() { | ||
a.should.eql(2); | ||
b.should.eql(4); | ||
c.should.eql(6); | ||
d.should.eql(8); | ||
done(); | ||
}); | ||
(next(function() { a = 2; }))(); | ||
(next(function() { a = 2; }))(); | ||
setTimeout(next(function() { | ||
b = 4; | ||
}, 100)); | ||
setTimeout(next(function() { | ||
b = 4; | ||
}, 100)); | ||
setTimeout(next(function() { | ||
c = 6; | ||
}, 300)); | ||
setTimeout(next(function() { | ||
c = 6; | ||
}, 300)); | ||
setTimeout(next(function() { | ||
d = 8; | ||
}, 200)); | ||
}); | ||
setTimeout(next(function() { | ||
d = 8; | ||
}, 200)); | ||
test('should pass the arguments to the original callbacks', function(t) { | ||
var next = afterAll(function() { | ||
t.end(); | ||
}); | ||
it('should pass the arguments to the original callbacks', function(done) { | ||
var next = afterAll(function() { | ||
done(); | ||
}); | ||
(next(function(a) { t.equal(a, 2)}))(2); | ||
(next(function(b) { t.equal(b, 'hi')}))('hi'); | ||
}); | ||
(next(function(a) { a.should.eql(2)}))(2); | ||
(next(function(b) { b.should.eql('hi')}))('hi'); | ||
test('should work if the callback is not passed', function(t) { | ||
var next = afterAll(function() { | ||
t.end(); | ||
}); | ||
it('should work if the callback is not passed', function(done) { | ||
var next = afterAll(function() { | ||
done(); | ||
}); | ||
setTimeout(next(), 300); | ||
}); | ||
setTimeout(next(), 300); | ||
test('should throw an error if the "next" function is called after the final callback is called', function(t) { | ||
var next = afterAll(function() {}); | ||
next()(); | ||
process.nextTick(function() { | ||
try { | ||
next(); | ||
} catch(e) { | ||
t.end(); | ||
} | ||
}); | ||
}); | ||
it('should throw an error if the "next" function is called after the final callback is called', function(done) { | ||
var next = afterAll(function() {}); | ||
next()(); | ||
test('should call the callback if the "next" function is never called in the same tick', function(t) { | ||
var next = afterAll(t.end.bind(t)); | ||
process.nextTick(function() {}); | ||
}); | ||
process.nextTick(function() { | ||
try { | ||
next(); | ||
} catch(e) { | ||
done(); | ||
} | ||
}); | ||
test('should catch errors and pass it to the final callback', function(t) { | ||
var next = afterAll(function(err) { | ||
t.ok(err); | ||
t.end(); | ||
}); | ||
it('should call the callback if the "next" function is never called in the same tick', function(done) { | ||
var next = afterAll(done); | ||
process.nextTick(function() {}); | ||
var n1 = next(); | ||
var n2 = next(); | ||
setTimeout(function() { | ||
n1(new Error('Some error')); | ||
}, 100); | ||
setTimeout(n2, 500); | ||
}); | ||
test('should only call the final callback once in the case of an error', function(t) { | ||
var count = 0; | ||
var next = afterAll(function(err) { | ||
t.equal(err.message, 'Oops!'); | ||
t.equal(++count, 1); | ||
t.end(); | ||
}); | ||
it('should catch errors and pass it to the final callback', function(done) { | ||
var next = afterAll(function(err) { | ||
err.should.be.ok; | ||
done(); | ||
}); | ||
var n1 = next(); | ||
var n2 = next(); | ||
var n3 = next(); | ||
var n1 = next(); | ||
var n2 = next(); | ||
n1(); | ||
n2(new Error('Oops!')); | ||
n3(new Error('Oops! 2')); | ||
setTimeout(function() { | ||
n1(new Error('Some error')); | ||
}, 100); | ||
setTimeout(n2, 10000); | ||
}); | ||
test('should call all the callbacks even in case of error', function(t) { | ||
var count = 0; | ||
var next = afterAll(function() { | ||
t.equal(count, 3); | ||
t.end(); | ||
}); | ||
var countup = function() { | ||
count++; | ||
}; | ||
var n1 = next(countup); | ||
var n2 = next(countup); | ||
var n3 = next(countup); | ||
it('should only call the final callback once in the case of an error', function(done) { | ||
var count = 0; | ||
var next = afterAll(function() { | ||
(++count === 1).should.be.ok; | ||
done(); | ||
}); | ||
n1(); | ||
n2(new Error('Oops!')); | ||
n3(new Error('Oops!')); | ||
var n1 = next(); | ||
var n2 = next(); | ||
var n3 = next(); | ||
}); | ||
n1(); | ||
n2(new Error('Oops!')); | ||
n3(new Error('Oops!')); | ||
}); | ||
test('should not require the final callback', function(t) { | ||
var next = afterAll(); | ||
it('should not require the final callback', function(done) { | ||
var next = afterAll(); | ||
var n1 = next(); | ||
var n2 = next(); | ||
var n3 = next(); | ||
var n1 = next(); | ||
var n2 = next(); | ||
var n3 = next(); | ||
n1(); | ||
n2(); | ||
n3(); | ||
n1(); | ||
n2(); | ||
n3(); | ||
setTimeout(function() { | ||
t.end(); | ||
}, 250); | ||
setTimeout(done, 250); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7854
1
159
1
+ Addedonce@^1.3.0
+ Addedonce@1.4.0(transitive)
+ Addedwrappy@1.0.2(transitive)