Created
February 4, 2019 02:23
-
-
Save bcherny/029473f21833a73126d2e1dce53f2a6a to your computer and use it in GitHub Desktop.
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
/* | |
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