Skip to content

Commit cf1d68d

Browse files
committed
Remove the unnecessary async handler (in Expr5)
1 parent e9d08b0 commit cf1d68d

File tree

4 files changed

+48
-52
lines changed

4 files changed

+48
-52
lines changed

controllers/authorController.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ const Author = require("../models/author");
22
const Book = require("../models/book");
33

44
const { body, validationResult } = require("express-validator");
5-
const asyncHandler = require("express-async-handler");
65

76
// Display list of all Authors.
8-
exports.author_list = asyncHandler(async (req, res, next) => {
7+
exports.author_list = async (req, res, next) => {
98
const allAuthors = await Author.find().sort({ family_name: 1 }).exec();
109
res.render("author_list", {
1110
title: "Author List",
1211
author_list: allAuthors,
1312
});
14-
});
13+
};
1514

1615
// Display detail page for a specific Author.
17-
exports.author_detail = asyncHandler(async (req, res, next) => {
16+
exports.author_detail = async (req, res, next) => {
1817
// Get details of author and all their books (in parallel)
1918
const [author, allBooksByAuthor] = await Promise.all([
2019
Author.findById(req.params.id).exec(),
@@ -33,7 +32,7 @@ exports.author_detail = asyncHandler(async (req, res, next) => {
3332
author,
3433
author_books: allBooksByAuthor,
3534
});
36-
});
35+
};
3736

3837
// Display Author create form on GET.
3938
exports.author_create_get = (req, res, next) => {
@@ -67,7 +66,7 @@ exports.author_create_post = [
6766
.toDate(),
6867

6968
// Process request after validation and sanitization.
70-
asyncHandler(async (req, res, next) => {
69+
async (req, res, next) => {
7170
// Extract the validation errors from a request.
7271
const errors = validationResult(req);
7372

@@ -93,11 +92,11 @@ exports.author_create_post = [
9392
await author.save();
9493
// Redirect to new author record.
9594
res.redirect(author.url);
96-
}),
95+
},
9796
];
9897

9998
// Display Author delete form on GET.
100-
exports.author_delete_get = asyncHandler(async (req, res, next) => {
99+
exports.author_delete_get = async (req, res, next) => {
101100
// Get details of author and all their books (in parallel)
102101
const [author, allBooksByAuthor] = await Promise.all([
103102
Author.findById(req.params.id).exec(),
@@ -114,10 +113,10 @@ exports.author_delete_get = asyncHandler(async (req, res, next) => {
114113
author,
115114
author_books: allBooksByAuthor,
116115
});
117-
});
116+
};
118117

119118
// Handle Author delete on POST.
120-
exports.author_delete_post = asyncHandler(async (req, res, next) => {
119+
exports.author_delete_post = async (req, res, next) => {
121120
// Get details of author and all their books (in parallel)
122121
const [author, allBooksByAuthor] = await Promise.all([
123122
Author.findById(req.params.id).exec(),
@@ -137,10 +136,10 @@ exports.author_delete_post = asyncHandler(async (req, res, next) => {
137136
// Author has no books. Delete object and redirect to the list of authors.
138137
await Author.findByIdAndDelete(req.body.authorid);
139138
res.redirect("/catalog/authors");
140-
});
139+
};
141140

142141
// Display Author update form on GET.
143-
exports.author_update_get = asyncHandler(async (req, res, next) => {
142+
exports.author_update_get = async (req, res, next) => {
144143
const author = await Author.findById(req.params.id).exec();
145144
if (author === null) {
146145
// No results.
@@ -150,7 +149,7 @@ exports.author_update_get = asyncHandler(async (req, res, next) => {
150149
}
151150

152151
res.render("author_form", { title: "Update Author", author });
153-
});
152+
};
154153

155154
// Handle Author update on POST.
156155
exports.author_update_post = [
@@ -179,7 +178,7 @@ exports.author_update_post = [
179178
.toDate(),
180179

181180
// Process request after validation and sanitization.
182-
asyncHandler(async (req, res, next) => {
181+
async (req, res, next) => {
183182
// Extract the validation errors from a request.
184183
const errors = validationResult(req);
185184

@@ -205,5 +204,5 @@ exports.author_update_post = [
205204
// Data from form is valid. Update the record.
206205
await Author.findByIdAndUpdate(req.params.id, author);
207206
res.redirect(author.url);
208-
}),
207+
},
209208
];

controllers/bookController.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ const Genre = require("../models/genre");
44
const BookInstance = require("../models/bookinstance");
55

66
const { body, validationResult } = require("express-validator");
7-
const asyncHandler = require("express-async-handler");
87

9-
exports.index = asyncHandler(async (req, res, next) => {
8+
exports.index = async (req, res, next) => {
109
// Get details of books, book instances, authors and genre counts (in parallel)
1110
const [
1211
numBooks,
@@ -30,20 +29,20 @@ exports.index = asyncHandler(async (req, res, next) => {
3029
author_count: numAuthors,
3130
genre_count: numGenres,
3231
});
33-
});
32+
};
3433

3534
// Display list of all books.
36-
exports.book_list = asyncHandler(async (req, res, next) => {
35+
exports.book_list = async (req, res, next) => {
3736
const allBooks = await Book.find({}, "title author")
3837
.sort({ title: 1 })
3938
.populate("author")
4039
.exec();
4140

4241
res.render("book_list", { title: "Book List", book_list: allBooks });
43-
});
42+
};
4443

4544
// Display detail page for a specific book.
46-
exports.book_detail = asyncHandler(async (req, res, next) => {
45+
exports.book_detail = async (req, res, next) => {
4746
// Get details of books, book instances for specific book
4847
const [book, bookInstances] = await Promise.all([
4948
Book.findById(req.params.id).populate("author").populate("genre").exec(),
@@ -62,10 +61,10 @@ exports.book_detail = asyncHandler(async (req, res, next) => {
6261
book,
6362
book_instances: bookInstances,
6463
});
65-
});
64+
};
6665

6766
// Display book create form on GET.
68-
exports.book_create_get = asyncHandler(async (req, res, next) => {
67+
exports.book_create_get = async (req, res, next) => {
6968
// Get all authors and genres, which we can use for adding to our book.
7069
const [allAuthors, allGenres] = await Promise.all([
7170
Author.find().sort({ family_name: 1 }).exec(),
@@ -77,7 +76,7 @@ exports.book_create_get = asyncHandler(async (req, res, next) => {
7776
authors: allAuthors,
7877
genres: allGenres,
7978
});
80-
});
79+
};
8180

8281
// Handle book create on POST.
8382
exports.book_create_post = [
@@ -107,7 +106,7 @@ exports.book_create_post = [
107106
body("genre.*").escape(),
108107
// Process request after validation and sanitization.
109108

110-
asyncHandler(async (req, res, next) => {
109+
async (req, res, next) => {
111110
// Extract the validation errors from a request.
112111
const errors = validationResult(req);
113112

@@ -148,11 +147,11 @@ exports.book_create_post = [
148147
// Data from form is valid. Save book.
149148
await book.save();
150149
res.redirect(book.url);
151-
}),
150+
},
152151
];
153152

154153
// Display book delete form on GET.
155-
exports.book_delete_get = asyncHandler(async (req, res, next) => {
154+
exports.book_delete_get = async (req, res, next) => {
156155
const [book, bookInstances] = await Promise.all([
157156
Book.findById(req.params.id).populate("author").populate("genre").exec(),
158157
BookInstance.find({ book: req.params.id }).exec(),
@@ -168,10 +167,10 @@ exports.book_delete_get = asyncHandler(async (req, res, next) => {
168167
book,
169168
book_instances: bookInstances,
170169
});
171-
});
170+
};
172171

173172
// Handle book delete on POST.
174-
exports.book_delete_post = asyncHandler(async (req, res, next) => {
173+
exports.book_delete_post = async (req, res, next) => {
175174
// Assume the post has valid id (ie no validation/sanitization).
176175

177176
const [book, bookInstances] = await Promise.all([
@@ -197,10 +196,10 @@ exports.book_delete_post = asyncHandler(async (req, res, next) => {
197196
// Book has no BookInstance objects. Delete object and redirect to the list of books.
198197
await Book.findByIdAndDelete(req.body.id);
199198
res.redirect("/catalog/books");
200-
});
199+
};
201200

202201
// Display book update form on GET.
203-
exports.book_update_get = asyncHandler(async (req, res, next) => {
202+
exports.book_update_get = async (req, res, next) => {
204203
// Get book, authors and genres for form.
205204
const [book, allAuthors, allGenres] = await Promise.all([
206205
Book.findById(req.params.id).populate("author").exec(),
@@ -226,7 +225,7 @@ exports.book_update_get = asyncHandler(async (req, res, next) => {
226225
genres: allGenres,
227226
book,
228227
});
229-
});
228+
};
230229

231230
// Handle book update on POST.
232231
exports.book_update_post = [
@@ -256,7 +255,7 @@ exports.book_update_post = [
256255
body("genre.*").escape(),
257256

258257
// Process request after validation and sanitization.
259-
asyncHandler(async (req, res, next) => {
258+
async (req, res, next) => {
260259
// Extract the validation errors from a request.
261260
const errors = validationResult(req);
262261

@@ -299,5 +298,5 @@ exports.book_update_post = [
299298
const thebook = await Book.findByIdAndUpdate(req.params.id, book, {});
300299
// Redirect to book detail page.
301300
res.redirect(thebook.url);
302-
}),
301+
},
303302
];

controllers/bookinstanceController.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ const BookInstance = require("../models/bookinstance");
22
const Book = require("../models/book");
33

44
const { body, validationResult } = require("express-validator");
5-
const asyncHandler = require("express-async-handler");
65

76
// Display list of all BookInstances.
8-
exports.bookinstance_list = asyncHandler(async (req, res, next) => {
7+
exports.bookinstance_list = async (req, res, next) => {
98
const allBookInstances = await BookInstance.find().populate("book").exec();
109

1110
res.render("bookinstance_list", {
1211
title: "Book Instance List",
1312
bookinstance_list: allBookInstances,
1413
});
15-
});
14+
};
1615

1716
// Display detail page for a specific BookInstance.
18-
exports.bookinstance_detail = asyncHandler(async (req, res, next) => {
17+
exports.bookinstance_detail = async (req, res, next) => {
1918
const bookInstance = await BookInstance.findById(req.params.id)
2019
.populate("book")
2120
.exec();
@@ -31,17 +30,17 @@ exports.bookinstance_detail = asyncHandler(async (req, res, next) => {
3130
title: "Book:",
3231
bookinstance: bookInstance,
3332
});
34-
});
33+
};
3534

3635
// Display BookInstance create form on GET.
37-
exports.bookinstance_create_get = asyncHandler(async (req, res, next) => {
36+
exports.bookinstance_create_get = async (req, res, next) => {
3837
const allBooks = await Book.find({}, "title").sort({ title: 1 }).exec();
3938

4039
res.render("bookinstance_form", {
4140
title: "Create BookInstance",
4241
book_list: allBooks,
4342
});
44-
});
43+
};
4544

4645
// Handle BookInstance create on POST.
4746
exports.bookinstance_create_post = [
@@ -58,7 +57,7 @@ exports.bookinstance_create_post = [
5857
.toDate(),
5958

6059
// Process request after validation and sanitization.
61-
asyncHandler(async (req, res, next) => {
60+
async (req, res, next) => {
6261
// Extract the validation errors from a request.
6362
const errors = validationResult(req);
6463

@@ -88,11 +87,11 @@ exports.bookinstance_create_post = [
8887
// Data from form is valid
8988
await bookInstance.save();
9089
res.redirect(bookInstance.url);
91-
}),
90+
},
9291
];
9392

9493
// Display BookInstance delete form on GET.
95-
exports.bookinstance_delete_get = asyncHandler(async (req, res, next) => {
94+
exports.bookinstance_delete_get = async (req, res, next) => {
9695
const bookInstance = await BookInstance.findById(req.params.id)
9796
.populate("book")
9897
.exec();
@@ -106,17 +105,17 @@ exports.bookinstance_delete_get = asyncHandler(async (req, res, next) => {
106105
title: "Delete BookInstance",
107106
bookinstance: bookInstance,
108107
});
109-
});
108+
};
110109

111110
// Handle BookInstance delete on POST.
112-
exports.bookinstance_delete_post = asyncHandler(async (req, res, next) => {
111+
exports.bookinstance_delete_post = async (req, res, next) => {
113112
// Assume valid BookInstance id in field.
114113
await BookInstance.findByIdAndDelete(req.body.id);
115114
res.redirect("/catalog/bookinstances");
116-
});
115+
};
117116

118117
// Display BookInstance update form on GET.
119-
exports.bookinstance_update_get = asyncHandler(async (req, res, next) => {
118+
exports.bookinstance_update_get = async (req, res, next) => {
120119
// Get book, all books for form (in parallel)
121120
const [bookInstance, allBooks] = await Promise.all([
122121
BookInstance.findById(req.params.id).populate("book").exec(),
@@ -136,7 +135,7 @@ exports.bookinstance_update_get = asyncHandler(async (req, res, next) => {
136135
selected_book: bookInstance.book._id,
137136
bookinstance: bookInstance,
138137
});
139-
});
138+
};
140139

141140
// Handle BookInstance update on POST.
142141
exports.bookinstance_update_post = [
@@ -153,7 +152,7 @@ exports.bookinstance_update_post = [
153152
.toDate(),
154153

155154
// Process request after validation and sanitization.
156-
asyncHandler(async (req, res, next) => {
155+
async (req, res, next) => {
157156
// Extract the validation errors from a request.
158157
const errors = validationResult(req);
159158

@@ -186,5 +185,5 @@ exports.bookinstance_update_post = [
186185
await BookInstance.findByIdAndUpdate(req.params.id, bookInstance, {});
187186
// Redirect to detail page.
188187
res.redirect(bookInstance.url);
189-
}),
188+
},
190189
];

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"cookie-parser": "^1.4.7",
1717
"debug": "^4.4.1",
1818
"express": "^5.1.0",
19-
"express-async-handler": "^1.2.0",
2019
"express-rate-limit": "^7.5.1",
2120
"express-validator": "^7.2.1",
2221
"helmet": "^8.1.0",

0 commit comments

Comments
 (0)