Atomic Save Pattern
NodeJS
Hard
4 views
Problem Description
Write JSON to a temp file then rename, print SAVED.
Output Format
Print SAVED.
Constraints
Use writeFileSync + renameSync.
Official Solution
const fs = require('fs');
const data = { site: 'meetcode', updatedAt: Date.now() };
const tmp = 'data.tmp.json';
const target = 'data.json';
fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf8');
fs.renameSync(tmp, target);
console.log('SAVED');
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!