Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
computeIntensiveRunner.js
const {promisify} = require('util')
const crypto = require('crypto')
function randomString() {
return crypto.randomBytes(100).toString('hex');
}
function* computeIntensiveCode() {
const hash = crypto.createHash('sha256');
for (let i=0; i < 100; i++) {
hash.update(randomString())
yield
}
return hash.digest('hex')
}
async function computeIntensiveRunner(generator) {
for(;;) {
const {done, value} = generator.next()
if (done) {
return value
}
await promisify(setImmediate)()
}
}
computeIntensiveRunner(computeIntensiveCode()).then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment