Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
991 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | # inflight
|
---|
| 2 |
|
---|
| 3 | Add callbacks to requests in flight to avoid async duplication
|
---|
| 4 |
|
---|
| 5 | ## USAGE
|
---|
| 6 |
|
---|
| 7 | ```javascript
|
---|
| 8 | var inflight = require('inflight')
|
---|
| 9 |
|
---|
| 10 | // some request that does some stuff
|
---|
| 11 | function req(key, callback) {
|
---|
| 12 | // key is any random string. like a url or filename or whatever.
|
---|
| 13 | //
|
---|
| 14 | // will return either a falsey value, indicating that the
|
---|
| 15 | // request for this key is already in flight, or a new callback
|
---|
| 16 | // which when called will call all callbacks passed to inflightk
|
---|
| 17 | // with the same key
|
---|
| 18 | callback = inflight(key, callback)
|
---|
| 19 |
|
---|
| 20 | // If we got a falsey value back, then there's already a req going
|
---|
| 21 | if (!callback) return
|
---|
| 22 |
|
---|
| 23 | // this is where you'd fetch the url or whatever
|
---|
| 24 | // callback is also once()-ified, so it can safely be assigned
|
---|
| 25 | // to multiple events etc. First call wins.
|
---|
| 26 | setTimeout(function() {
|
---|
| 27 | callback(null, key)
|
---|
| 28 | }, 100)
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // only assigns a single setTimeout
|
---|
| 32 | // when it dings, all cbs get called
|
---|
| 33 | req('foo', cb1)
|
---|
| 34 | req('foo', cb2)
|
---|
| 35 | req('foo', cb3)
|
---|
| 36 | req('foo', cb4)
|
---|
| 37 | ```
|
---|
Note:
See
TracBrowser
for help on using the repository browser.