From d0cda5e679d5ba42cd495ec4164f85bc7faaf792 Mon Sep 17 00:00:00 2001 From: ItsQuadrus Date: Wed, 20 Mar 2024 20:04:40 +0100 Subject: [PATCH] asynchronous file system operations --- server.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/server.js b/server.js index 7b446c9..39b9e41 100644 --- a/server.js +++ b/server.js @@ -12,13 +12,16 @@ let pixels = new Array(canvasWidth * canvasHeight).fill(initialPixelColor); let totalPixelsPlaced = 0; // Counter for total pixels placed by everyone // Function to save the canvas data to a JSON file -function saveCanvasToJSON() { - const data = JSON.stringify(pixels); - fs.writeFileSync('canvas_data.json', data, 'utf8', (err) => { - if (err) { - console.error('Error saving canvas data:', err); - } - }); +async function saveCanvasToJSON() { + try { + await fs.promises.access('canvas_data.json', fs.constants.F_OK); + } catch (err) { + // Create the file if it doesn't exist + await fs.promises.writeFile('canvas_data.json', JSON.stringify(pixels), 'utf8'); + } + + // Write the updated pixel data to the JSON file + await fs.promises.writeFile('canvas_data.json', JSON.stringify(pixels), 'utf8'); } // Function to load the canvas data from the JSON file