Skip to content

Commit 77123aa

Browse files
committed
Removed abstraction in asynchronous processing and rewrote it in a direct form
独自の抽象関数を使用せず、非同期処理を直接記述する形式に修正 - コールバックベースの処理をそのまま記述 - 非同期処理の流れが分かりやすくなるよう変更 - for文を使用せず、レコード追加を追加分そのまま記述
1 parent f1c8683 commit 77123aa

File tree

2 files changed

+61
-97
lines changed

2 files changed

+61
-97
lines changed

03.asynchronous/sqlite_callback_error.js

+40-52
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,46 @@ const selectAllQuery = "SELECT * FROM movies";
1212
const deleteTableQuery = "DROP TABLE books";
1313

1414
const titles = ["I Am a Cat", "I Am a Cat", "SANSHIRO"];
15-
let insertCount = 0;
1615

17-
function main() {
18-
createTable();
19-
}
20-
21-
function createTable() {
22-
db.run(createTableQuery, insertTitles);
23-
}
24-
25-
function insertTitles() {
16+
db.run(createTableQuery, function () {
2617
console.log("Table created");
27-
for (const title of titles) {
28-
db.run(insertTitleQuery, [title], displayID);
29-
}
30-
}
31-
32-
function displayID(err) {
33-
if (err) {
34-
console.error(`Error occurred while inserting record: ${err.message}`);
35-
} else {
36-
console.log(`Record inserted successfully with ID: ${this.lastID}`);
37-
}
38-
insertCount++;
39-
if (insertCount === titles.length) {
40-
fetchAll();
41-
}
42-
}
43-
44-
function fetchAll() {
45-
db.all(selectAllQuery, displayAll);
46-
}
47-
48-
function displayAll(err, rows) {
49-
if (err) {
50-
console.error(`Error occurred while fetching records: ${err.message}`);
51-
} else {
52-
console.log("All records fetched successfully");
53-
for (const row of rows) {
54-
console.log(`id:${row.id}, title:${row.title}`);
18+
db.run(insertTitleQuery, titles[0], function (err) {
19+
if (err) {
20+
console.error(`Error occurred while inserting record: ${err.message}`);
21+
} else {
22+
console.log(`Record inserted successfully with ID: ${this.lastID}`);
5523
}
56-
}
57-
deleteTable();
58-
}
59-
60-
function deleteTable() {
61-
db.run(deleteTableQuery, closeDB);
62-
}
63-
64-
function closeDB() {
65-
console.log("Table deleted");
66-
db.close();
67-
}
68-
69-
main();
24+
db.run(insertTitleQuery, titles[1], function (err) {
25+
if (err) {
26+
console.error(`Error occurred while inserting record: ${err.message}`);
27+
} else {
28+
console.log(`Record inserted successfully with ID: ${this.lastID}`);
29+
}
30+
db.run(insertTitleQuery, titles[2], function (err) {
31+
if (err) {
32+
console.error(
33+
`Error occurred while inserting record: ${err.message}`,
34+
);
35+
} else {
36+
console.log(`Record inserted successfully with ID: ${this.lastID}`);
37+
}
38+
db.all(selectAllQuery, function (err, rows) {
39+
if (err) {
40+
console.error(
41+
`Error occurred while fetching records: ${err.message}`,
42+
);
43+
} else {
44+
console.log("All records fetched successfully");
45+
for (const row of rows) {
46+
console.log(`id:${row.id}, title:${row.title}`);
47+
}
48+
}
49+
db.run(deleteTableQuery, function () {
50+
console.log("Table deleted");
51+
db.close();
52+
});
53+
});
54+
});
55+
});
56+
});
57+
});

03.asynchronous/sqlite_callback_no_error.js

+21-45
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,26 @@ const selectAllQuery = "SELECT * FROM books";
1212
const deleteTableQuery = "DROP TABLE books";
1313

1414
const titles = ["I Am a Cat", "KOKORO", "SANSHIRO"];
15-
let insertCount = 0;
1615

17-
function main() {
18-
createTable();
19-
}
20-
21-
function createTable() {
22-
db.run(createTableQuery, insertTitles);
23-
}
24-
25-
function insertTitles() {
16+
db.run(createTableQuery, function () {
2617
console.log("Table created");
27-
for (const title of titles) {
28-
db.run(insertTitleQuery, [title], displayID);
29-
}
30-
}
31-
32-
function displayID() {
33-
console.log(`Record inserted successfully with ID: ${this.lastID}`);
34-
insertCount++;
35-
if (insertCount === titles.length) {
36-
fetchAll();
37-
}
38-
}
39-
40-
function fetchAll() {
41-
db.all(selectAllQuery, displayAll);
42-
}
43-
44-
function displayAll(err, rows) {
45-
console.log("All records fetched successfully");
46-
for (const row of rows) {
47-
console.log(`id:${row.id}, title:${row.title}`);
48-
}
49-
deleteTable();
50-
}
51-
52-
function deleteTable() {
53-
db.run(deleteTableQuery, closeDB);
54-
}
55-
56-
function closeDB() {
57-
console.log("Table deleted");
58-
db.close();
59-
}
60-
61-
main();
18+
db.run(insertTitleQuery, titles[0], function () {
19+
console.log(`Record inserted successfully with ID: ${this.lastID}`);
20+
db.run(insertTitleQuery, titles[1], function () {
21+
console.log(`Record inserted successfully with ID: ${this.lastID}`);
22+
db.run(insertTitleQuery, titles[2], function () {
23+
console.log(`Record inserted successfully with ID: ${this.lastID}`);
24+
db.all(selectAllQuery, function (err, rows) {
25+
console.log("All records fetched successfully");
26+
for (const row of rows) {
27+
console.log(`id:${row.id}, title:${row.title}`);
28+
}
29+
db.run(deleteTableQuery, function () {
30+
console.log("Table deleted");
31+
db.close();
32+
});
33+
});
34+
});
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)