forked from bobbyconnolly/node-express-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.js
More file actions
29 lines (24 loc) · 752 Bytes
/
async.js
File metadata and controls
29 lines (24 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const getDataFromFacebook = () =>
new Promise((resolve, reject) => {
setTimeout(() => {
if (new Date().getTime() % 10) {
resolve("some data from Facebook")
} else {
reject("unable to reach server")
}
}, 500)
})
const middleware3 = async (req, res, next) => {
// Be sure to use this try-catch-next pattern for async error handling (or use Koa instead of Express)
// https://stackoverflow.com/questions/51391080/handling-errors-in-express-async-middleware
try {
const data = await getDataFromFacebook()
res.locals.data = data
next()
} catch (error) {
next(error)
}
}
module.exports = {
middleware3,
}