Created
April 19, 2017 16:05
-
-
Save andest01/248b0e1c842e4e06d9231448c08b16cf to your computer and use it in GitHub Desktop.
throttleReduce
This file contains 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
async function throttleReduce(ops) { | |
let resolvedResults = [] | |
for (let i = 0; i < ops.length; i++) { | |
let op = ops[i] | |
let result = await waitForNext(op.bind(null, i)) | |
resolvedResults.push(result) | |
} | |
return resolvedResults | |
} | |
function waitForNext(op) { | |
return new Promise((resolve, reject) => { | |
requestAnimationFrame(() => { | |
try { | |
let result = op() | |
console.log(result) | |
resolve(result) | |
} catch (error) { | |
reject(error) | |
} | |
}) | |
}) | |
} | |
function dummyOp(val) { | |
for (let i = 0; i < 20000000; i++) { | |
Math.random() | |
} | |
return val | |
} | |
const ops = _.times(600, () => dummyOp) | |
// call this method to kick off the whole enchilada | |
const DoStuff = async () => { | |
let results = await throttleReduce(ops) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment