wip: add more questions

chore/javascript
Arik Chakma 1 year ago
parent 4d0143f137
commit 0393a658a7
  1. 16
      src/data/question-groups/javascript/content/hoisting.md
  2. 51
      src/data/question-groups/javascript/content/promise-all-vs-all-settled.md
  3. 18
      src/data/question-groups/javascript/javascript.md

@ -0,0 +1,16 @@
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that no matter where the functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local. Note that hoisting only moves the declaration, not the initialization.
```js
console.log(x === undefined); // true
var x = 3;
console.log(x); // 3
```
The above code snippet can be visualized in the following way:
```js
var x;
console.log(x === undefined); // true
x = 3;
console.log(x); // 3
```

@ -0,0 +1,51 @@
The core difference between `Promise.all()` and `Promise.allSettled()` is that `Promise.all()` rejects immediately if any of the promises reject whereas `Promise.allSettled()` waits for all of the promises to settle (either resolve or reject) and then returns the result.
## Initialize
```js
const promise1 = Promise.resolve('Promise 1 resolved');
const promise2 = Promise.reject('Promise 2 rejected');
```
## Using `Promise.all()`
```js
Promise.all([promise1, promise2])
.then((values) => {
console.log(values);
})
.catch((error) => {
console.log('An error occurred in Promise.all():', error);
});
// Output:
// An error occurred in Promise.all(): Promise 2 rejected
```
In the above code, the `Promise.all()` rejects immediately when any of the `promise2` rejects.
## Using `Promise.allSettled()`
```js
Promise.allSettled([promise1, promise2]).then((results) => {
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
console.log(
`Promise ${index + 1} was fulfilled with value:`,
result.value
);
} else {
console.log(
`Promise ${index + 1} was rejected with reason:`,
result.reason
);
}
});
});
// Output:
// Promise 1 was fulfilled with value: Promise 1 resolved
// Promise 2 was rejected with reason: Promise 2 rejected
```
In the above code, the `Promise.allSettled()` waits for all of the promises to settle (either resolve or reject) and then returns the result.

@ -57,6 +57,7 @@ questions:
answer: custom-event.md answer: custom-event.md
topics: topics:
- 'Core' - 'Core'
- 'DOM'
- 'Advanced' - 'Advanced'
- question: What is a closure in JavaScript? - question: What is a closure in JavaScript?
answer: closure.md answer: closure.md
@ -162,6 +163,7 @@ questions:
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' - 'Core'
- '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
@ -177,5 +179,21 @@ questions:
answer: prevent-default.md answer: prevent-default.md
topics: topics:
- 'Core' - 'Core'
- 'DOM'
- 'Intermediate' - 'Intermediate'
- question: What is Hoisting in JavaScript?
answer: hoisting.md
topics:
- 'Core'
- 'Intermediate'
- question: What is DOM?
answer: The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects.
topics:
- 'DOM'
- 'Beginner'
- question: Difference between `Promise.all()` and `Promise.allSettled()`?
answer: promise-all-vs-all-settled.md
topics:
- 'Core'
- 'Advanced'
--- ---

Loading…
Cancel
Save