index.js 685 B

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