The difference between Asynchronous and Synchronous code is that Asynchronous code does not block the execution of the program while Synchronous code does.
## Asynchronous code
Asynchronous code is executed in the background and it does not block the execution of the program. It is usually used to perform tasks that take a long time to complete, such as network requests.
```js
console.log('Before');
setTimeout(() => {
console.log('Hello');
}, 1000);
console.log('After');
```
## Synchronous code
Synchronous code is executed in sequence and it blocks the execution of the program until it is completed. If a task takes a long time to complete, everything else waits.
In order to handle errors in promises, we can use the `catch` method or the second argument of the `then` method.
## Rejecting a promise
```js
const promise = new Promise((resolve, reject) => {
reject(new Error('Something went wrong'));
});
```
## Catch method
In this method, we can pass a `callback` function that will be called when the promise is `rejected`.
```js
promise
.then((result) => {
console.log(result);
})
.catch((error) => {
console.log(error.message);
});
```
## Second argument of the then method
In this method, we can pass two `callback` functions as arguments. The first one will be called when the promise is `resolved` and the second one will be called when the promise is `rejected`.
- question: How to implement your own Custom Event in JavaScript?
- question: How to implement your own Custom Event in JavaScript?
answer: custom-event.md
answer: custom-event.md
topics:
topics:
- 'Core'
- 'Event'
- 'DOM'
- 'Advanced'
- 'Advanced'
- question: What is a closure in JavaScript?
- question: What is a closure in JavaScript?
answer: closure.md
answer: closure.md
@ -67,7 +66,7 @@ questions:
- question: Does Arrow functions have their own `this`?
- question: Does Arrow functions have their own `this`?
answer: No, arrow functions do not have their own `this`. Instead, they inherit the `this` of the enclosing lexical scope.
answer: No, arrow functions do not have their own `this`. Instead, they inherit the `this` of the enclosing lexical scope.
topics:
topics:
- 'Core'
- 'Function'
- 'Intermediate'
- 'Intermediate'
- question: Does `map()` method mutate the original array?
- question: Does `map()` method mutate the original array?
answer: map-method.md
answer: map-method.md
@ -107,7 +106,7 @@ questions:
- question: What is IIFE in JavaScript?
- question: What is IIFE in JavaScript?
answer: iife.md
answer: iife.md
topics:
topics:
- 'Core'
- 'Function'
- 'Advanced'
- 'Advanced'
- question: What is Inheritance in JavaScript?
- question: What is Inheritance in JavaScript?
answer: inheritance.md
answer: inheritance.md
@ -117,12 +116,12 @@ questions:
- question: What is Map in JavaScript?
- question: What is Map in JavaScript?
answer: map.md
answer: map.md
topics:
topics:
- 'Core'
- 'Date Type'
- 'Beginner'
- 'Beginner'
- question: What is Set in JavaScript?
- question: What is Set in JavaScript?
answer: set.md
answer: set.md
topics:
topics:
- 'Core'
- 'Data Type'
- 'Beginner'
- 'Beginner'
- question: How you can find unique values in an array?
- question: How you can find unique values in an array?
answer: find-unique-array-values.md
answer: find-unique-array-values.md
@ -132,12 +131,12 @@ questions:
- question: What is a JavaScript promise?
- question: What is a JavaScript promise?
answer: A Promise in JavaScript represents a value that may not be available yet but will be at some point. Promises provide a way to handle asynchronous operations, offering methods like `.then()` and `.catch()` to register callbacks for success and failure.
answer: A Promise in JavaScript represents a value that may not be available yet but will be at some point. Promises provide a way to handle asynchronous operations, offering methods like `.then()` and `.catch()` to register callbacks for success and failure.
topics:
topics:
- 'Core'
- 'Promise'
- 'Advanced'
- 'Advanced'
- question: What is the purpose of the `async/await` in JavaScript?
- question: What is the purpose of the `async/await` in JavaScript?
answer: The `async/await`, introduced in ES2017, provides a more readable and cleaner way to handle asynchronous operations compared to callbacks and promises. An `async` function always returns a promise, and within such a function, you can use `await` to pause execution until a promise settles.
answer: The `async/await`, introduced in ES2017, provides a more readable and cleaner way to handle asynchronous operations compared to callbacks and promises. An `async` function always returns a promise, and within such a function, you can use `await` to pause execution until a promise settles.
topics:
topics:
- 'Core'
- 'Promise'
- 'Advanced'
- 'Advanced'
- question: What is callback hell in JavaScript?
- question: What is callback hell in JavaScript?
answer: callback-hell.md
answer: callback-hell.md
@ -152,18 +151,17 @@ questions:
- question: Explain `alert()`, `prompt()`, and `confirm()` methods in JavaScript?
- question: Explain `alert()`, `prompt()`, and `confirm()` methods in JavaScript?
answer: alert-prompt-confirm.md
answer: alert-prompt-confirm.md
topics:
topics:
- 'Core'
- 'Event'
- 'Intermediate'
- 'Intermediate'
- question: How to handle event bubbling in JavaScript?
- question: How to handle event bubbling in JavaScript?
answer: event-bubbling.md
answer: event-bubbling.md
topics:
topics:
- 'Core'
- 'Event'
- 'Beginner'
- 'Beginner'
- question: What is Event Capturing in JavaScript?
- question: What is Event Capturing in JavaScript?
answer: Event capturing is the first phase of event propagation. In this phase, the event is captured by the outermost element and propagated to the inner elements. It is also known as trickling. It is the opposite of event bubbling.
answer: Event capturing is the first phase of event propagation. In this phase, the event is captured by the outermost element and propagated to the inner elements. It is also known as trickling. It is the opposite of event bubbling.
topics:
topics:
- 'Core'
- 'Event'
- 'DOM'
- 'Beginner'
- 'Beginner'
- question: What is the spread operator in JavaScript?
- question: What is the spread operator in JavaScript?
answer: spread-operator.md
answer: spread-operator.md
@ -178,8 +176,7 @@ questions:
- question: What is `preventDefault()` method in JavaScript?
- question: What is `preventDefault()` method in JavaScript?
answer: prevent-default.md
answer: prevent-default.md
topics:
topics:
- 'Core'
- 'Event'
- 'DOM'
- 'Intermediate'
- 'Intermediate'
- question: What is Hoisting in JavaScript?
- question: What is Hoisting in JavaScript?
answer: hoisting.md
answer: hoisting.md
@ -194,12 +191,12 @@ questions:
- question: Difference between `Promise.all()` and `Promise.allSettled()`?
- question: Difference between `Promise.all()` and `Promise.allSettled()`?
answer: promise-all-vs-all-settled.md
answer: promise-all-vs-all-settled.md
topics:
topics:
- 'Core'
- 'Promise'
- 'Advanced'
- 'Advanced'
- question: What is the difference between `Map` and `WeakMap` in JavaScript?
- question: What is the difference between `Map` and `WeakMap` in JavaScript?
answer: The `Map` object holds key-value pairs and remembers the original insertion order of the keys. Whereas, the `WeakMap` object is a collection of key/value pairs in which the keys are weakly referenced. You can use any data type as a key or value in a `Map` whereas in `WeakMap` you can only use objects as keys. The `WeakMap` is not iterable whereas `Map` is. In `WeakMap` it holds the weak reference to the original object which means if there are no other references to an object stored in the `WeakMap`, those objects can be garbage collected.
answer: The `Map` object holds key-value pairs and remembers the original insertion order of the keys. Whereas, the `WeakMap` object is a collection of key/value pairs in which the keys are weakly referenced. You can use any data type as a key or value in a `Map` whereas in `WeakMap` you can only use objects as keys. The `WeakMap` is not iterable whereas `Map` is. In `WeakMap` it holds the weak reference to the original object which means if there are no other references to an object stored in the `WeakMap`, those objects can be garbage collected.
topics:
topics:
- 'Core'
- 'Data Type'
- 'Advanced'
- 'Advanced'
- question: Garbage collection in JavaScript?
- question: Garbage collection in JavaScript?
answer: The JavaScript engine uses automatic garbage collection. JavaScript automatically manages memory by freeing up space used by objects no longer needed. This algorithm is called Mark and Sweep, which is performed periodically by the JavaScript engine.
answer: The JavaScript engine uses automatic garbage collection. JavaScript automatically manages memory by freeing up space used by objects no longer needed. This algorithm is called Mark and Sweep, which is performed periodically by the JavaScript engine.
@ -214,12 +211,12 @@ questions:
- question: What is Type Casting?
- question: What is Type Casting?
answer: Type conversion (or typecasting) means transfer of data from one data type to another. Implicit conversion happens when the compiler (for compiled languages) or runtime (for script languages like `JavaScript`) automatically converts data types.
answer: Type conversion (or typecasting) means transfer of data from one data type to another. Implicit conversion happens when the compiler (for compiled languages) or runtime (for script languages like `JavaScript`) automatically converts data types.
topics:
topics:
- 'Core'
- 'Data Type'
- 'Intermediate'
- 'Intermediate'
- question: What are Explicit binding in JavaScript?
- question: What are Explicit binding in JavaScript?
answer: explicit-binding.md
answer: explicit-binding.md
topics:
topics:
- 'Core'
- 'Function'
- 'Advanced'
- 'Advanced'
- question: How to run a piece of code after a specific time interval?
- question: How to run a piece of code after a specific time interval?
answer: set-interval.md
answer: set-interval.md
@ -249,7 +246,7 @@ questions:
- question: How to accept variable number of arguments in a JavaScript function?
- question: How to accept variable number of arguments in a JavaScript function?
answer: variable-number-of-arguments.md
answer: variable-number-of-arguments.md
topics:
topics:
- 'Core'
- 'Function'
- 'Intermediate'
- 'Intermediate'
- question: How to define multiline strings in JavaScript?
- question: How to define multiline strings in JavaScript?
answer: In order to define multiline strings in JavaScript, you need to use template literals. Template literals are enclosed by the backtick (```` ` ` ````) character instead of double or single quotes. Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (``` `${expression}` ```).
answer: In order to define multiline strings in JavaScript, you need to use template literals. Template literals are enclosed by the backtick (```` ` ` ````) character instead of double or single quotes. Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (``` `${expression}` ```).
@ -259,7 +256,7 @@ questions:
- question: Uses of `break` and `continue` statements in JavaScript?
- question: Uses of `break` and `continue` statements in JavaScript?
answer: break-and-continue.md
answer: break-and-continue.md
topics:
topics:
- 'Core'
- 'Loop'
- 'Beginner'
- 'Beginner'
- question: How to parse JSON in JavaScript?
- question: How to parse JSON in JavaScript?
answer: parse-json.md
answer: parse-json.md
@ -268,6 +265,26 @@ questions:
- 'Beginner'
- 'Beginner'
- question: How to debug JavaScript code?
- question: How to debug JavaScript code?
answer: debug-javascript.md
answer: debug-javascript.md
topics:
- 'Debug'
- 'Beginner'
- question: How to handle error in Promise?
answer: error-in-promise.md
topics:
- 'Promise'
- 'Advanced'
- question: How to handle error in async/await?
answer: error-in-async-await.md
topics:
- 'Promise'
- 'Advanced'
- question: How to use `finally` block in Promise?