diff --git a/lib/storage.js b/lib/storage.js index 74daf08..d244733 100755 --- a/lib/storage.js +++ b/lib/storage.js @@ -89,14 +89,18 @@ storage.crashSafeWriteFileLines = (filename, lines, callback = () => {}) => { }, cb => { try { - const stream = fs.createWriteStream(tempFilename, { - flags: 'w' - }) - for (const line of lines) { - stream.write(line) - stream.write('\n') + const stream = fs.createWriteStream(tempFilename) + const next = () => { + const line = lines.shift() + if (line === undefined) { + stream.close(cb) + } else { + stream.write(line) + stream.write('\n') + setImmediate(next) + } } - stream.close(() => cb()) + next() } catch (err) { cb(err) }