Created
March 5, 2018 19:18
-
-
Save andrejewski/bfae59169e206622bb37ec968b4d4742 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function createLoader (keysTask) { | |
| let keys = [] | |
| let nextTaskPromise | |
| return { | |
| load (key) { | |
| const index = keys.push(key) - 1; | |
| if (!nextTaskPromise) { | |
| let resolver | |
| nextTaskPromise = new Promise((resolve, reject) => { | |
| resolver = { resolve, reject }; | |
| }); | |
| setTimeout(() => { | |
| keysTask(keys).then(resolver.resolve, resolver.reject); | |
| keys = []; | |
| nextTaskPromise = null; | |
| }, 50); | |
| } | |
| return nextTaskPromise.then(results => results[index]); | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment