Skip to content

Commit 389aad6

Browse files
committed
Featured: new questions
1 parent d5639f2 commit 389aad6

File tree

39 files changed

+1035
-3
lines changed

39 files changed

+1035
-3
lines changed

Diff for: README.md

+227-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

2-
# javascript-questions-pro (580 questions)
2+
# javascript-questions-pro (590 questions)
33

4-
## [Levels](./level/) (3)
4+
## [Levels](./level/) (4)
55
- [basic](./level/basic)
66
- [intermediate](./level/intermediate)
77
- [advanced](./level/advanced)
8+
- [beginner](./level/beginner)
89

9-
## [Themes](./theme/) (209)
10+
## [Themes](./theme/) (211)
1011
- [indexeddb](./theme/indexeddb)
1112
- [db](./theme/db)
1213
- [storage](./theme/storage)
@@ -216,6 +217,8 @@
216217
- [verification](./theme/verification)
217218
- [alerts](./theme/alerts)
218219
- [headless testing](./theme/headless_testing)
220+
- [async await](./theme/async_await)
221+
- [express js](./theme/express_js)
219222

220223
## [Tutorials with Videos](./video/) (191)
221224
- [What is IndexedDB](https://www.tiktok.com/@jsmentoring/photo/7448276165661314336)
@@ -12460,4 +12463,225 @@ capabilities: {
1246012463
1246112464
1246212465
12466+
---
12467+
12468+
### How to catch errors in Promises
12469+
12470+
You can handle errors in promises by appending a `.catch()` method to the promise chain.
12471+
12472+
```javascript
12473+
fetch('https://api.example.com/data')
12474+
.then(response => response.json())
12475+
.then(data => console.log(data))
12476+
.catch(error => console.error('Error:', error));
12477+
```
12478+
12479+
**Tags**: [beginner](./level/beginner), [JavaScript](./theme/javascript), [Promises](./theme/promises), [Error Handling](./theme/error_handling)
12480+
12481+
12482+
12483+
---
12484+
12485+
### How to catch errors in async/await
12486+
12487+
Wrap your async code in a try/catch block to handle errors gracefully.
12488+
12489+
```javascript
12490+
async function fetchData() {
12491+
try {
12492+
const response = await fetch('https://api.example.com/data');
12493+
const data = await response.json();
12494+
console.log(data);
12495+
} catch (error) {
12496+
console.error('Error:', error);
12497+
}
12498+
}
12499+
fetchData();
12500+
```
12501+
12502+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Async/Await](./theme/async_await), [Error Handling](./theme/error_handling)
12503+
12504+
12505+
12506+
---
12507+
12508+
### How to catch errors in synchronous code
12509+
12510+
For synchronous operations, use a try/catch block to handle errors.
12511+
12512+
```javascript
12513+
try {
12514+
const result = riskyOperation();
12515+
console.log(result);
12516+
} catch (error) {
12517+
console.error('Caught an error:', error);
12518+
}
12519+
```
12520+
12521+
**Tags**: [beginner](./level/beginner), [JavaScript](./theme/javascript), [Error Handling](./theme/error_handling)
12522+
12523+
12524+
12525+
---
12526+
12527+
### How to catch errors in event handlers
12528+
12529+
Wrap event handler code in a try/catch block to ensure errors do not crash the application.
12530+
12531+
```javascript
12532+
document.getElementById('myButton').addEventListener('click', () => {
12533+
try {
12534+
// Code that might throw an error
12535+
performAction();
12536+
} catch (error) {
12537+
console.error('Error during click event:', error);
12538+
}
12539+
});
12540+
```
12541+
12542+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Error Handling](./theme/error_handling), [Events](./theme/events)
12543+
12544+
12545+
12546+
---
12547+
12548+
### How to catch errors with the Fetch API
12549+
12550+
Use `.catch()` to handle errors when making HTTP requests with the Fetch API.
12551+
12552+
```javascript
12553+
fetch('https://api.example.com/data')
12554+
.then(response => {
12555+
if (!response.ok) {
12556+
throw new Error('Network response was not ok');
12557+
}
12558+
return response.json();
12559+
})
12560+
.then(data => console.log(data))
12561+
.catch(error => console.error('Fetch error:', error));
12562+
```
12563+
12564+
**Tags**: [beginner](./level/beginner), [JavaScript](./theme/javascript), [Fetch API](./theme/fetch_api), [Error Handling](./theme/error_handling)
12565+
12566+
12567+
12568+
---
12569+
12570+
### How to catch errors in Node.js
12571+
12572+
In Node.js, use try/catch for synchronous code and attach error listeners for asynchronous code.
12573+
12574+
```javascript
12575+
// Synchronous error handling
12576+
try {
12577+
const data = fs.readFileSync('/path/to/file');
12578+
console.log(data);
12579+
} catch (error) {
12580+
console.error('Error reading file:', error);
12581+
}
12582+
12583+
// Asynchronous error handling
12584+
fs.readFile('/path/to/file', (error, data) => {
12585+
if (error) {
12586+
return console.error('Error reading file:', error);
12587+
}
12588+
console.log(data);
12589+
});
12590+
```
12591+
12592+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Node.js](./theme/node_js), [Error Handling](./theme/error_handling)
12593+
12594+
12595+
12596+
---
12597+
12598+
### How to catch errors in callbacks
12599+
12600+
When working with callbacks, ensure that errors are passed as the first argument to the callback function.
12601+
12602+
```javascript
12603+
function performAsyncOperation(callback) {
12604+
// Simulate error
12605+
const error = new Error('Something went wrong');
12606+
callback(error, null);
12607+
}
12608+
12609+
performAsyncOperation((error, result) => {
12610+
if (error) {
12611+
return console.error('Callback error:', error);
12612+
}
12613+
console.log(result);
12614+
});
12615+
```
12616+
12617+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Callbacks](./theme/callbacks), [Error Handling](./theme/error_handling)
12618+
12619+
12620+
12621+
---
12622+
12623+
### How to use global error handling with try/catch
12624+
12625+
For comprehensive error handling, set up global error listeners in your application.
12626+
12627+
```javascript
12628+
window.onerror = function(message, source, lineno, colno, error) {
12629+
console.error('Global error caught:', message, 'at', source + ':' + lineno + ':' + colno);
12630+
};
12631+
12632+
// Example triggering a global error
12633+
nonExistentFunction();
12634+
```
12635+
12636+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Error Handling](./theme/error_handling)
12637+
12638+
12639+
12640+
---
12641+
12642+
### How to catch errors in Express.js
12643+
12644+
Use middleware to catch and handle errors in your Express applications.
12645+
12646+
```javascript
12647+
const express = require('express');
12648+
const app = express();
12649+
12650+
// Your routes here
12651+
app.get('/', (req, res) => {
12652+
throw new Error('Oops!');
12653+
});
12654+
12655+
// Error handling middleware
12656+
app.use((err, req, res, next) => {
12657+
console.error(err.stack);
12658+
res.status(500).send('Something broke!');
12659+
});
12660+
12661+
app.listen(3000, () => console.log('Server running on port 3000'));
12662+
```
12663+
12664+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Express.js](./theme/express_js), [Error Handling](./theme/error_handling)
12665+
12666+
12667+
12668+
---
12669+
12670+
### How to catch unhandled promise rejections
12671+
12672+
Listen for unhandled promise rejections to prevent unexpected application crashes.
12673+
12674+
```javascript
12675+
process.on('unhandledRejection', (reason, promise) => {
12676+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
12677+
});
12678+
12679+
// Example of an unhandled rejection
12680+
Promise.reject(new Error('Unhandled error'));
12681+
```
12682+
12683+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Promises](./theme/promises), [Error Handling](./theme/error_handling)
12684+
12685+
12686+
1246312687
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## [How to catch unhandled promise rejections](#how-to-catch-unhandled-rejections)
2+
3+
### How to catch unhandled promise rejections
4+
5+
Listen for unhandled promise rejections to prevent unexpected application crashes.
6+
7+
```javascript
8+
process.on('unhandledRejection', (reason, promise) => {
9+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
10+
});
11+
12+
// Example of an unhandled rejection
13+
Promise.reject(new Error('Unhandled error'));
14+
```
15+
16+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Promises](./theme/promises), [Error Handling](./theme/error_handling)
17+
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## [How to use global error handling with try/catch](#how-to-use-global-error-handling-with-try-catch)
2+
3+
### How to use global error handling with try/catch
4+
5+
For comprehensive error handling, set up global error listeners in your application.
6+
7+
```javascript
8+
window.onerror = function(message, source, lineno, colno, error) {
9+
console.error('Global error caught:', message, 'at', source + ':' + lineno + ':' + colno);
10+
};
11+
12+
// Example triggering a global error
13+
nonExistentFunction();
14+
```
15+
16+
**Tags**: [advanced](./level/advanced), [JavaScript](./theme/javascript), [Error Handling](./theme/error_handling)
17+
18+

Diff for: level/beginner/how_to_catch_errors_in_promises.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## [How to catch errors in Promises](#how-to-catch-errors-in-promises)
2+
3+
### How to catch errors in Promises
4+
5+
You can handle errors in promises by appending a `.catch()` method to the promise chain.
6+
7+
```javascript
8+
fetch('https://api.example.com/data')
9+
.then(response => response.json())
10+
.then(data => console.log(data))
11+
.catch(error => console.error('Error:', error));
12+
```
13+
14+
**Tags**: [beginner](./level/beginner), [JavaScript](./theme/javascript), [Promises](./theme/promises), [Error Handling](./theme/error_handling)
15+
16+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## [How to catch errors in synchronous code](#how-to-catch-errors-in-synchronous-code)
2+
3+
### How to catch errors in synchronous code
4+
5+
For synchronous operations, use a try/catch block to handle errors.
6+
7+
```javascript
8+
try {
9+
const result = riskyOperation();
10+
console.log(result);
11+
} catch (error) {
12+
console.error('Caught an error:', error);
13+
}
14+
```
15+
16+
**Tags**: [beginner](./level/beginner), [JavaScript](./theme/javascript), [Error Handling](./theme/error_handling)
17+
18+
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## [How to catch errors with the Fetch API](#how-to-catch-errors-with-fetch-api)
2+
3+
### How to catch errors with the Fetch API
4+
5+
Use `.catch()` to handle errors when making HTTP requests with the Fetch API.
6+
7+
```javascript
8+
fetch('https://api.example.com/data')
9+
.then(response => {
10+
if (!response.ok) {
11+
throw new Error('Network response was not ok');
12+
}
13+
return response.json();
14+
})
15+
.then(data => console.log(data))
16+
.catch(error => console.error('Fetch error:', error));
17+
```
18+
19+
**Tags**: [beginner](./level/beginner), [JavaScript](./theme/javascript), [Fetch API](./theme/fetch_api), [Error Handling](./theme/error_handling)
20+
21+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## [How to catch errors in async/await](#how-to-catch-errors-in-async-await)
2+
3+
### How to catch errors in async/await
4+
5+
Wrap your async code in a try/catch block to handle errors gracefully.
6+
7+
```javascript
8+
async function fetchData() {
9+
try {
10+
const response = await fetch('https://api.example.com/data');
11+
const data = await response.json();
12+
console.log(data);
13+
} catch (error) {
14+
console.error('Error:', error);
15+
}
16+
}
17+
fetchData();
18+
```
19+
20+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Async/Await](./theme/async_await), [Error Handling](./theme/error_handling)
21+
22+
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## [How to catch errors in callbacks](#how-to-catch-errors-in-callbacks)
2+
3+
### How to catch errors in callbacks
4+
5+
When working with callbacks, ensure that errors are passed as the first argument to the callback function.
6+
7+
```javascript
8+
function performAsyncOperation(callback) {
9+
// Simulate error
10+
const error = new Error('Something went wrong');
11+
callback(error, null);
12+
}
13+
14+
performAsyncOperation((error, result) => {
15+
if (error) {
16+
return console.error('Callback error:', error);
17+
}
18+
console.log(result);
19+
});
20+
```
21+
22+
**Tags**: [intermediate](./level/intermediate), [JavaScript](./theme/javascript), [Callbacks](./theme/callbacks), [Error Handling](./theme/error_handling)
23+
24+

0 commit comments

Comments
 (0)