186cde4db0a6bc1571520f9c53cf4510f9c46d95d870357f1b843b72a2340a2e1313f6a99e71c691487fee071627c22ed1fb834ee6e5bb2cddc379a1ce5d41 991 B

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