From 4b2839092d58cedf5a3410b1f8829d44c04b81e6 Mon Sep 17 00:00:00 2001 From: Mitsuo Sato Date: Thu, 11 Nov 2021 13:30:20 +0900 Subject: [PATCH] =?UTF-8?q?=E7=B7=B4=E7=BF=92=E5=95=8F=E9=A1=8C=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 21 +++++++++++++++++---- app2.js | 16 ++++++++++++++++ app3.js | 19 +++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 app2.js create mode 100644 app3.js diff --git a/app.js b/app.js index e5b6b400..6a7cfb33 100644 --- a/app.js +++ b/app.js @@ -1,8 +1,21 @@ 'use strict'; const fs = require('fs'); const fileName = './test.txt'; -for (let count = 0; count < 30; count++) { - fs.appendFile(fileName, 'おはようございます\n', 'utf8', () => {}); - fs.appendFile(fileName, 'こんにちは\n', 'utf8', () => {}); - fs.appendFile(fileName, 'こんばんは\n', 'utf8', () => {}); + +function appendFilePromise(fileName, str) { + return new Promise((resolve) => { + fs.appendFile(fileName, str, 'utf8', () => resolve()); + }); } +async function main() { + for (let count = 0; count < 500; count++) { + await appendFilePromise(fileName, 'あ'); + await appendFilePromise(fileName, 'い'); + await appendFilePromise(fileName, 'う'); + await appendFilePromise(fileName, 'え'); + await appendFilePromise(fileName, 'お'); + await appendFilePromise(fileName, '\n'); + } +} + +main(); \ No newline at end of file diff --git a/app2.js b/app2.js new file mode 100644 index 00000000..c904de84 --- /dev/null +++ b/app2.js @@ -0,0 +1,16 @@ +'use strict'; +const fs = require('fs'); +const fileName = './test.txt'; + +function appendFilePromise(fileName, str){ + return new Promise((resolve) =>{ + fs.appendFile(fileName, str, 'utf8', () => resolve()); + }); +} +async function main() { + for (let count = 0; count < 30; count++) { + await appendFilePromise(fileName, 'おはようございます\n'); + await appendFilePromise(fileName, 'こんにちは\n'); + await appendFilePromise(fileName, 'こんばんは\n'); + } +} \ No newline at end of file diff --git a/app3.js b/app3.js new file mode 100644 index 00000000..0347c57e --- /dev/null +++ b/app3.js @@ -0,0 +1,19 @@ +'use strict'; +const fs = require('fs'); +const fileName = './test.txt'; + +function appendFilePromise(fileName, str) { + return new Promise((resolve) => { + fs.appendFile(fileName, str, 'utf8', () => resolve()); + }); +} + +for (let count = 0; count < 30; count++) { + appendFilePromise(fileName, 'おはようございます\n') + .then(() => { + return appendFilePromise(fileName, 'こんにちは\n'); + }) + .then(() => { + return appendFilePromise(fileName, 'こんばんは\n'); + }); +} \ No newline at end of file