From c01f5b6fd9feaecbba56c644c68f88c88de9e19d Mon Sep 17 00:00:00 2001 From: ShokiYokota Date: Fri, 8 Apr 2022 18:15:01 +0900 Subject: [PATCH 1/5] =?UTF-8?q?promise=E3=81=AE=E5=8B=89=E5=BC=B7=E3=81=A7?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- promise-test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 promise-test.js diff --git a/promise-test.js b/promise-test.js new file mode 100644 index 0000000..f382f8c --- /dev/null +++ b/promise-test.js @@ -0,0 +1,24 @@ +'use strict' + +new Promise((resolve) => { + const nowDate = new Date(); + resolve(nowDate); + }).then((v1) => { + // v1 は 現在の時刻情報 + new Promise((resolve) => { + const monthAndDate = { + month: v1.getMonth(), + date: v1.getDate() + } + resolve(monthAndDate); + }).then((v2) => { + // v2 は 日付の情報 + new Promise((resolve) => { + const text = `今日は${v2.month+1}月${v2.date}日です。`; + resolve(text); + }).then((v3) => { + // v3 は 日付を示す文章 + console.log(v3); // 今日の日付に関する文章が出力される + }); + }); + }); \ No newline at end of file From a7e6999f84e21ba200f0e7c60a3dfec8e545819d Mon Sep 17 00:00:00 2001 From: ShokiYokota Date: Fri, 8 Apr 2022 18:23:59 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89?= =?UTF-8?q?=E3=83=81=E3=82=A7=E3=82=A4=E3=83=B3=E3=81=A7=E6=8F=8F=E3=81=8D?= =?UTF-8?q?=E7=9B=B4=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- promise-test.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/promise-test.js b/promise-test.js index f382f8c..bbf5b65 100644 --- a/promise-test.js +++ b/promise-test.js @@ -3,22 +3,25 @@ new Promise((resolve) => { const nowDate = new Date(); resolve(nowDate); - }).then((v1) => { - // v1 は 現在の時刻情報 - new Promise((resolve) => { + }) + .then((v1) => { + // v1 は 現在の時刻情報 const monthAndDate = { month: v1.getMonth(), date: v1.getDate() } - resolve(monthAndDate); - }).then((v2) => { + return new Promise((resolve) => { + resolve(monthAndDate); + }); + }) + .then((v2) => { // v2 は 日付の情報 - new Promise((resolve) => { - const text = `今日は${v2.month+1}月${v2.date}日です。`; + const text = `今日は${v2.month+1}月${v2.date}日です。`; + return new Promise((resolve) => { resolve(text); - }).then((v3) => { - // v3 は 日付を示す文章 - console.log(v3); // 今日の日付に関する文章が出力される }); - }); - }); \ No newline at end of file + }) + .then((v3) => { + // v3 は 日付を示す文章 + console.log(v3); // 今日の日付に関する文章が出力される + }); \ No newline at end of file From b8d07ede1eb27c58916ef3c29b4f5a0ad0175d81 Mon Sep 17 00:00:00 2001 From: ShokiYokota Date: Fri, 8 Apr 2022 18:39:53 +0900 Subject: [PATCH 3/5] =?UTF-8?q?async=20/await=E3=82=92=E7=94=A8=E3=81=84?= =?UTF-8?q?=E3=81=A6=E5=90=8C=E6=9C=9F=E5=87=A6=E7=90=86=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 5b239dd..885a2b7 100644 --- a/app.js +++ b/app.js @@ -1,11 +1,21 @@ 'use strict'; const fs = require('fs'); const fileName = './test.txt'; -for (let count = 0; count < 500; count++) { - fs.appendFile(fileName, 'あ', 'utf8', () => {}); - fs.appendFile(fileName, 'い', 'utf8', () => {}); - fs.appendFile(fileName, 'う', 'utf8', () => {}); - fs.appendFile(fileName, 'え', 'utf8', () => {}); - fs.appendFile(fileName, 'お', 'utf8', () => {}); - fs.appendFile(fileName, '\n', 'utf8', () => {}); -} \ No newline at end of file + +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(); From 9d73f8438ccf159cccb89d9fd09df75647bc7089 Mon Sep 17 00:00:00 2001 From: ShokiYokota Date: Fri, 8 Apr 2022 18:45:40 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=E9=9D=9E=E5=90=8C=E6=9C=9F=E3=81=AE?= =?UTF-8?q?=E3=82=82=E3=81=AE=E3=82=92=E4=B8=80=E6=97=A6=E3=82=B3=E3=83=9F?= =?UTF-8?q?=E3=83=83=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- async-test.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 async-test.js diff --git a/async-test.js b/async-test.js new file mode 100644 index 0000000..6fac8b1 --- /dev/null +++ b/async-test.js @@ -0,0 +1,8 @@ +'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', () => {}); +} \ No newline at end of file From a31509f95f0161679cdf6ce315a5a0a005bbaad1 Mon Sep 17 00:00:00 2001 From: ShokiYokota Date: Fri, 8 Apr 2022 19:04:22 +0900 Subject: [PATCH 5/5] =?UTF-8?q?appendFileSync=E3=82=92=E4=BD=BF=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E5=90=8C=E6=9C=9F=E5=87=A6=E7=90=86=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- async-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/async-test.js b/async-test.js index 6fac8b1..e84d8fe 100644 --- a/async-test.js +++ b/async-test.js @@ -2,7 +2,7 @@ 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', () => {}); + fs.appendFileSync(fileName, 'おはようございます\n', 'utf8'); + fs.appendFileSync(fileName, 'こんにちは\n', 'utf8'); + fs.appendFileSync(fileName, 'こんばんは\n', 'utf8' ); } \ No newline at end of file