From 605f623505366f037635eb3962a4c4b7984d7b33 Mon Sep 17 00:00:00 2001 From: Kazu Date: Wed, 31 Dec 2025 13:20:27 +0900 Subject: [PATCH] =?UTF-8?q?promise,=20async,=20await=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 --- Dockerfile | 9 +++++++++ app.js | 12 +++++------- async-await-intro.js | 37 +++++++++++++++++++++++++++++++++++++ promise-intro.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100644 async-await-intro.js create mode 100644 promise-intro.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7c9c5de --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM --platform=linux/x86_64 node:18.15.0-slim + +RUN apt-get update && \ + apt-get install -y locales git procps less +RUN locale-gen ja_JP.UTF-8 +RUN localedef -f UTF-8 -i ja_JP ja_JP +ENV LANG=ja_JP.UTF-8 +ENV TZ=Asia/Tokyo +WORKDIR /async-io-problem diff --git a/app.js b/app.js index ad67e03..b9d5768 100644 --- a/app.js +++ b/app.js @@ -10,15 +10,13 @@ https.get('https://www.nicovideo.jp/ranking/genre/all?term=hour&rss=2.0&lang=ja- 'User-Agent': 'node', }, }, - (response) => { - response + (res) => { + res .on('data', (chunk) => { data += chunk; }) - .on('end', () => { - + .on('end', () => { // getが完了したらファイルにdataを保存 + fs.writeFile('test.txt', data, 'utf8', () => {}); }); } -); - -fs.writeFile('test.txt', data, 'utf8', () => {}); \ No newline at end of file +); \ No newline at end of file diff --git a/async-await-intro.js b/async-await-intro.js new file mode 100644 index 0000000..3cbc280 --- /dev/null +++ b/async-await-intro.js @@ -0,0 +1,37 @@ +'use strict'; + +const https = require('node:https'); +const fs = require('node:fs'); + +function getContent(url) { + return new Promise((resolve, reject) => { + let data = ''; + https.get(url, (res) => { + res + .on('data', (chunk) => { + data += chunk; + }) + .on('end', () => { + resolve(data); + }); + }); + }); +} + +// function save(path, content) { +// return new Promise((resolve, reject) => { +// fs.writeFile(path, content, 'utf8', () =>{ +// resolve('success'); +// }); +// }); +// } + +async function main() { + const url = 'https://www.nicovideo.jp/ranking/genre/all?term=hour&rss=2.0&lang=ja-jp'; + const path = 'test.txt'; + const content = await getContent(url); + fs.writeFileSync(path, content, 'utf8'); + console.log("success"); +} + +main(); \ No newline at end of file diff --git a/promise-intro.js b/promise-intro.js new file mode 100644 index 0000000..2b02ade --- /dev/null +++ b/promise-intro.js @@ -0,0 +1,30 @@ +'use strict'; + +const https = require('node:https'); +const fs = require('node:fs'); + +const promise = new Promise((resolve, reject) => { + let data =''; + https.get('https://www.nicovideo.jp/ranking/genre/all?term=hour&rss=2.0&lang=ja-jp', + (res) => { + console.log('status:', res.statusCode); + res + .on('data', (chunk) => { + data += chunk; // チャンク:データのかけら + }) + .on('end', () => { + resolve(data); + }); + } + ); +}); + +promise.then((data) => { + return new Promise((resolve, reject) => { + fs.writeFile('test.txt', data, 'utf8', () => { + resolve('success'); + }); + }); +}).then((message) => { + console.log(message); +}) \ No newline at end of file