Skip to content

Commit 00477c9

Browse files
authored
Create byeTryCatchErrorHandling.js
1 parent cb3c83c commit 00477c9

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// catchAwait.js
2+
const catchAwait = promise => (
3+
promise
4+
.then(data => ({ data, error: null }))
5+
.catch(error => ({ error, data: null }))
6+
);
7+
8+
module.exports = catchAwait;
9+
10+
// working file
11+
const { getItems } = require('./api/items');
12+
const catchAwait = require('./utils/catchAwait');
13+
14+
const allItems = async () => {
15+
const { error, data } = await catchAwait(getItems());
16+
if (!error) {
17+
// code
18+
}
19+
console.error(error);
20+
}
21+
22+
allItems();

0 commit comments

Comments
 (0)