Skip to content

Instantly share code, notes, and snippets.

@giltayar
Created March 6, 2019 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giltayar/9399c8bfa2090a98d91ec844dfd83fd7 to your computer and use it in GitHub Desktop.
Save giltayar/9399c8bfa2090a98d91ec844dfd83fd7 to your computer and use it in GitHub Desktop.
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