index.js (685B)
1 module.exports = after 2 3 function after(count, callback, err_cb) { 4 var bail = false 5 err_cb = err_cb || noop 6 proxy.count = count 7 8 return (count === 0) ? callback() : proxy 9 10 function proxy(err, result) { 11 if (proxy.count <= 0) { 12 throw new Error('after called too many times') 13 } 14 --proxy.count 15 16 // after first error, rest are passed to err_cb 17 if (err) { 18 bail = true 19 callback(err) 20 // future error callbacks will go to error handler 21 callback = err_cb 22 } else if (proxy.count === 0 && !bail) { 23 callback(null, result) 24 } 25 } 26 } 27 28 function noop() {}