Skip to content

Instantly share code, notes, and snippets.

@andest01
Created April 19, 2017 16:05
Show Gist options
  • Save andest01/248b0e1c842e4e06d9231448c08b16cf to your computer and use it in GitHub Desktop.
Save andest01/248b0e1c842e4e06d9231448c08b16cf to your computer and use it in GitHub Desktop.
throttleReduce
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