├── .gitignore ├── LICENSE ├── README.md ├── inflight.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | .#* 3 | coverage 4 | .nyc_* 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017, Rebecca Turner 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # promise-inflight 2 | 3 | One promise for multiple requests in flight to avoid async duplication 4 | 5 | ## USAGE 6 | 7 | ```javascript 8 | const inflight = require('promise-inflight') 9 | 10 | // some request that does some stuff 11 | function req(key) { 12 | // key is any random string. like a url or filename or whatever. 13 | return inflight(key, () => { 14 | // this is where you'd fetch the url or whatever 15 | return Promise.delay(100) 16 | }) 17 | } 18 | 19 | // only assigns a single setTimeout 20 | // when it dings, all thens get called with the same result. (There's only 21 | // one underlying promise.) 22 | req('foo').then(…) 23 | req('foo').then(…) 24 | req('foo').then(…) 25 | req('foo').then(…) 26 | ``` 27 | 28 | ## SEE ALSO 29 | 30 | * [inflight](https://npmjs.com/package/inflight) - For the callback based function on which this is based. 31 | 32 | ## STILL NEEDS 33 | 34 | Tests! 35 | -------------------------------------------------------------------------------- /inflight.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | module.exports = inflight 3 | 4 | let Bluebird 5 | try { 6 | Bluebird = require('bluebird') 7 | } catch (_) { 8 | Bluebird = Promise 9 | } 10 | 11 | const active = {} 12 | inflight.active = active 13 | function inflight (unique, doFly) { 14 | return Bluebird.all([unique, doFly]).then(function (args) { 15 | const unique = args[0] 16 | const doFly = args[1] 17 | if (Array.isArray(unique)) { 18 | return Bluebird.all(unique).then(function (uniqueArr) { 19 | return _inflight(uniqueArr.join(''), doFly) 20 | }) 21 | } else { 22 | return _inflight(unique, doFly) 23 | } 24 | }) 25 | 26 | function _inflight (unique, doFly) { 27 | if (!active[unique]) { 28 | active[unique] = (new Bluebird(function (resolve) { 29 | return resolve(doFly()) 30 | })) 31 | active[unique].then(cleanup, cleanup) 32 | function cleanup() { delete active[unique] } 33 | } 34 | return active[unique] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "promise-inflight", 3 | "version": "1.0.1", 4 | "description": "One promise for multiple requests in flight to avoid async duplication", 5 | "main": "inflight.js", 6 | "files": [ 7 | "inflight.js" 8 | ], 9 | "license": "ISC", 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "keywords": [], 14 | "author": "Rebecca Turner (http://re-becca.org/)", 15 | "devDependencies": {}, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/iarna/promise-inflight.git" 19 | }, 20 | "bugs": { 21 | "url": "https://github.com/iarna/promise-inflight/issues" 22 | }, 23 | "homepage": "https://github.com/iarna/promise-inflight#readme" 24 | } 25 | --------------------------------------------------------------------------------