|  | 
| 1 | 1 | 
 | 
| 2 |  | -# javascript-questions-pro (580 questions) | 
|  | 2 | +# javascript-questions-pro (590 questions) | 
| 3 | 3 | 
 | 
| 4 |  | -## [Levels](./level/) (3) | 
|  | 4 | +## [Levels](./level/) (4) | 
| 5 | 5 | - [basic](./level/basic) | 
| 6 | 6 | - [intermediate](./level/intermediate) | 
| 7 | 7 | - [advanced](./level/advanced) | 
|  | 8 | +- [beginner](./level/beginner) | 
| 8 | 9 | 
 | 
| 9 |  | -## [Themes](./theme/) (209)   | 
|  | 10 | +## [Themes](./theme/) (211)   | 
| 10 | 11 | - [indexeddb](./theme/indexeddb) | 
| 11 | 12 | - [db](./theme/db) | 
| 12 | 13 | - [storage](./theme/storage) | 
|  | 
| 216 | 217 | - [verification](./theme/verification) | 
| 217 | 218 | - [alerts](./theme/alerts) | 
| 218 | 219 | - [headless testing](./theme/headless_testing) | 
|  | 220 | +- [async await](./theme/async_await) | 
|  | 221 | +- [express js](./theme/express_js) | 
| 219 | 222 | 
 | 
| 220 | 223 | ## [Tutorials with Videos](./video/) (191) | 
| 221 | 224 | - [What is IndexedDB](https://www.tiktok.com/@jsmentoring/photo/7448276165661314336) | 
| @@ -12460,4 +12463,225 @@ capabilities: { | 
| 12460 | 12463 | 
 | 
| 12461 | 12464 | 
 | 
| 12462 | 12465 | 
 | 
|  | 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 | +
 | 
| 12463 | 12687 | --- | 
|  | 
0 commit comments