Skip to content

Instantly share code, notes, and snippets.

@bcherny
Created February 4, 2019 02:23
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 bcherny/029473f21833a73126d2e1dce53f2a6a to your computer and use it in GitHub Desktop.
Save bcherny/029473f21833a73126d2e1dce53f2a6a to your computer and use it in GitHub Desktop.
/*
Results of a few runs:
$ node ./index.js
{
"a": 1000
}
$ node ./index.js
{
"a": 999,
"ab": 1
}
$ node ./index.js
{
"a": 998,
"ab": 2
}
$ node ./index.js
{
"a": 995,
"ab": 5
}
*/
let {appendFile, readFile, writeFile} = require('fs')
let tally = {}
let globalDone = 0
let COUNT = 1000
for (let x = 0; x < COUNT; x++) {
let path = './tmp/' + Math.random() + '.txt'
// create file
writeFile(path, 'a', () => {
let result = ''
let done = 0
let checkDone = () => {
done++
if (done === 2) {
globalDone++
if (!(result in tally)) {
tally[result] = 0
}
tally[result]++
}
if (globalDone === COUNT) {
console.log(JSON.stringify(tally, null, 2))
}
}
readFile(path, {encoding: 'utf8'}, (error, data) => {
result = data
checkDone()
})
appendFile(path, 'b', (error) => {
checkDone()
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment