Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非同期処理プログラムの提出 #3

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f3d5f27
splite3をインストール
miyanew Dec 30, 2024
4e979ca
エラーなしエラーありをファイル分割して作成する
miyanew Jan 6, 2025
4d4cfe4
Promiseベースなsqlite3の関数を切り出す
miyanew Jan 6, 2025
9fac0f2
async版にもヘルパー関数を適用する
miyanew Jan 6, 2025
09ae015
捕捉したい例外のみを捕捉する
miyanew Jan 6, 2025
c67ad6a
エラーメッセージを標準エラー出力に出力する
miyanew Jan 7, 2025
03fda89
余計な空白を含まないようにSQLを1行でかく
miyanew Jan 7, 2025
661ad6d
自動採番された ID を出力する
miyanew Jan 7, 2025
883d4c5
ファイル名をプレフィックスのない名詞形にする
miyanew Jan 7, 2025
f01507a
実行順を制御できてないためコールバックベースの処理からループを除外する
miyanew Jan 9, 2025
54eacfb
DBクローズをPromiseとして扱えるようにする
miyanew Jan 10, 2025
956e3c3
不要なデフォルト引数を削除する
miyanew Jan 10, 2025
7e59a66
待機できてないのでpromiseベースの処理からループを除外する
miyanew Jan 10, 2025
31b4294
課題要件をみたすため参照エラーが起きるようにする
miyanew Jan 10, 2025
0ded943
不要なネストを修正する
miyanew Jan 11, 2025
c115759
アロー関数の簡易文体でそろえる
miyanew Jan 13, 2025
9d82436
Promiseの渡し漏れを直す
miyanew Jan 13, 2025
dc52ab1
async関数を定義せずTop-level awaitでシンプルに書く
miyanew Jan 13, 2025
0cc3111
catchで捕捉したオブジェクトがErrorであり指定プロパティをもつことを確認する
miyanew Jan 13, 2025
f39a6b9
フォーマッタの修正を反映する
miyanew Jan 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions 03.asynchronous/c1_callback.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

順番を管理する必要があるならしょうがないですが、必要がないならファイル名に prefix を付けるのはやめたほうがよいと思います。c1 が何を指しているのかもよく分からないです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

承知しました!

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sqlite3 from "sqlite3";

const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"];
const db = new sqlite3.Database(":memory:");

db.run(
`CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

複数行のテンプレート文字列でインデントすると、以下のようにインデント分も空白として含まれてしまいますよ。

CREATE TABLE books (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL UNIQUE
  )

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

余計な空白を含ませないようにSQLを1行で書くように修正しました!

() => {
let index = 0;

bookTitles.forEach((title) => {
db.run("INSERT INTO books (title) VALUES (?)", [title], () => {
console.log(`ADD TITLE: ${title}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自動採番された ID を出力してください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました!


db.get("SELECT * FROM books WHERE title = ?", [title], (_, book) => {
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`);
index++;

if (index >= bookTitles.length) {
db.run("DROP TABLE books", () => {
db.close();
});
}
});
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コールバックベースの非同期処理は、ループと組み合わせてその実行順を制御することはできないです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントありがとうございます。なるほど、これがコールバック形式の問題点なのですね!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ループ処理を除外しました!

},
);
31 changes: 31 additions & 0 deletions 03.asynchronous/c2_callback_contain_error.js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイル名は名詞形になるようにしてください。文のように見えます。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました!

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sqlite3 from "sqlite3";

const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"];
const db = new sqlite3.Database(":memory:");

db.run(
`CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)`,
() => {
let index = 0;

bookTitles.forEach((title) => {
db.run("INSERT INTO books (title) VALUES (?)", [title], () => {
console.log(`ADD TITLE: ${title}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーメッセージを標準エラー出力に出力してください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実装漏れ失礼しました🙇‍♂️
標準エラー出力の処理を追加しました。


db.get("SELECT * FROM books WHERE title = ?", [title], (_, book) => {
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーメッセージを標準エラー出力に出力してください。

Copy link
Owner Author

@miyanew miyanew Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実装漏れ失礼しました🙇‍♂️
標準エラー出力の処理を追加しました。

index++;

if (index >= bookTitles.length) {
db.run("DROP TABLE books", () => {
db.close();
});
}
});
});
});
},
);
36 changes: 36 additions & 0 deletions 03.asynchronous/c3_promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sqlite3 from "sqlite3";
import { run, get } from "./sqlite_utils.js";

const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"];
const db = new sqlite3.Database(":memory:");

run(
db,
`CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)`,
)
.then(() => {
let promise = Promise.resolve();

bookTitles.forEach((title) => {
promise = promise
.then(() =>
run(db, "INSERT INTO books (title) VALUES (?)", [title]).then(() =>
console.log(`ADD TITLE: ${title}`),
),
)
.then(() =>
get(db, "SELECT * FROM books WHERE title = ?", [title]).then(
(book) => {
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`);
},
),
);
});

return promise;
})
.then(() => run(db, "DROP TABLE books"))
.then(() => db.close());
40 changes: 40 additions & 0 deletions 03.asynchronous/c4_promise_contain_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sqlite3 from "sqlite3";
import { run, get } from "./sqlite_utils.js";

const bookTitles = ["booktitle_01", "booktitle_01", "booktitle_02"];
const db = new sqlite3.Database(":memory:");

run(
db,
`CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)`,
)
.then(() => {
let promise = Promise.resolve();

bookTitles.forEach((title) => {
promise = promise
.then(() =>
run(db, "INSERT INTO books (title) VALUES (?)", [title])
.then(() => console.log(`ADD TITLE: ${title}`))
.catch((err) => {
console.error(`ADD TITLE: ${title}, ${err.message}`);
}),
)
.then(() =>
get(db, "SELECT * FROM NotExistTable WHERE title = ?", [title])
.then((book) => {
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`);
})
.catch((err) => {
console.error(`GET TITLE: ${title}, ${err.message}`);
}),
);
});

return promise;
})
.then(() => run(db, "DROP TABLE books"))
.then(() => db.close());
28 changes: 28 additions & 0 deletions 03.asynchronous/c5_async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sqlite3 from "sqlite3";
import { run, get } from "./sqlite_utils.js";

const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"];
const db = new sqlite3.Database(":memory:");

const main = async () => {
await run(
db,
`CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)`,
);

for (const title of bookTitles) {
await run(db, "INSERT INTO books (title) VALUES (?)", [title]);
console.log(`ADD TITLE: ${title}`);

const book = await get(db, "SELECT * FROM books WHERE title = ?", [title]);
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`);
}

await run(db, "DROP TABLE books");
db.close();
};

main();
48 changes: 48 additions & 0 deletions 03.asynchronous/c6_async_contain_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sqlite3 from "sqlite3";
import { run, get } from "./sqlite_utils.js";

const bookTitles = ["booktitle_01", "booktitle_01", "booktitle_02"];
const db = new sqlite3.Database(":memory:");

const main = async () => {
await run(
db,
`CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)`,
);

for (const title of bookTitles) {
try {
await run(db, "INSERT INTO books (title) VALUES (?)", [title]);
console.log(`ADD TITLE: ${title}`);
} catch (err) {
if (err.code === "SQLITE_CONSTRAINT") {
console.error(`ADD TITLE: ${title}, ${err.message}`);
} else {
throw err;
}
}

try {
const book = await get(
db,
"SELECT * FROM NotExistTable WHERE title = ?",
[title],
);
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`);
} catch (err) {
if (err.code === "SQLITE_ERROR") {
console.error(`GET TITLE: ${title}, ${err.message}`);
} else {
throw err;
}
}
}

await run(db, "DROP TABLE books");
db.close();
};

main();
Loading