You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
685 B

7 years ago
  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() {}