Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
LICENSE | D | 12-May-2024 | 748 | 16 | 12 | |
README.md | D | 12-May-2024 | 991 | 38 | 30 | |
inflight.js | D | 12-May-2024 | 1.3 KiB | 55 | 40 | |
package.json | D | 12-May-2024 | 1.3 KiB | 63 | 62 |
README.md
1# inflight 2 3Add callbacks to requests in flight to avoid async duplication 4 5## USAGE 6 7```javascript 8var inflight = require('inflight') 9 10// some request that does some stuff 11function 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 33req('foo', cb1) 34req('foo', cb2) 35req('foo', cb3) 36req('foo', cb4) 37``` 38