inflight

Add callbacks to requests in flight to avoid async duplication

USAGE

  1. var inflight = require('inflight')
  2. // some request that does some stuff
  3. function req(key, callback) {
  4. // key is any random string. like a url or filename or whatever.
  5. //
  6. // will return either a falsey value, indicating that the
  7. // request for this key is already in flight, or a new callback
  8. // which when called will call all callbacks passed to inflightk
  9. // with the same key
  10. callback = inflight(key, callback)
  11. // If we got a falsey value back, then there's already a req going
  12. if (!callback) return
  13. // this is where you'd fetch the url or whatever
  14. // callback is also once()-ified, so it can safely be assigned
  15. // to multiple events etc. First call wins.
  16. setTimeout(function() {
  17. callback(null, key)
  18. }, 100)
  19. }
  20. // only assigns a single setTimeout
  21. // when it dings, all cbs get called
  22. req('foo', cb1)
  23. req('foo', cb2)
  24. req('foo', cb3)
  25. req('foo', cb4)