|
1512 | 1512 |
|
1513 | 1513 | 51. ### What is a promise
|
1514 | 1514 |
|
1515 |
| - A promise is an object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved(for example, network error). It will be in one of the 3 possible states: fulfilled, rejected, or pending. |
| 1515 | + A promise is an object that may produce a single value some time in the future with either a resolved value or a reason that it’s not resolved (i.e. rejected, that means for example, due to a network error). It will be in one of the 3 possible states: fulfilled, rejected, or pending. |
1516 | 1516 |
|
1517 | 1517 | The syntax of Promise creation looks like below,
|
1518 | 1518 |
|
|
1581 | 1581 |
|
1582 | 1582 | 55. ### Why do we need callbacks
|
1583 | 1583 |
|
1584 |
| - The callbacks are needed because javascript is an event driven language. That means instead of waiting for a response javascript will keep executing while listening for other events. |
| 1584 | + The callbacks are needed because javascript is an event driven language. That means instead of waiting for a response, javascript will keep executing while listening for other events. |
1585 | 1585 | Let's take an example with the first function invoking an API call(simulated by setTimeout) and the next function which logs the message.
|
1586 | 1586 |
|
1587 | 1587 | ```javascript
|
|
1597 | 1597 | firstFunction();
|
1598 | 1598 | secondFunction();
|
1599 | 1599 |
|
1600 |
| - Output; |
| 1600 | + // Output: |
1601 | 1601 | // Second function called
|
1602 | 1602 | // First function called
|
1603 | 1603 | ```
|
|
1626 | 1626 |
|
1627 | 1627 | 57. ### What are server-sent events
|
1628 | 1628 |
|
1629 |
| - Server-sent events (SSE) is a server push technology enabling a browser to receive automatic updates from a server via HTTP connection without resorting to polling. These are a one way communications channel - events flow from server to client only. This has been used in Facebook/Twitter updates, stock price updates, news feeds etc. |
| 1629 | + Server-sent events (SSE) is a server push technology enabling a browser to receive automatic updates from a server via HTTP connection without resorting to polling. These are a one way communications channel - events flow from server to client only. This has been used in Facebook/Twitter/X updates, stock price updates, news feeds etc. |
1630 | 1630 |
|
1631 | 1631 | **[⬆ Back to Top](#table-of-contents)**
|
1632 | 1632 |
|
|
1683 | 1683 |
|
1684 | 1684 | 62. ### What is callback in callback
|
1685 | 1685 |
|
1686 |
| - You can nest one callback inside in another callback to execute the actions sequentially one by one. This is known as callbacks in callbacks. |
| 1686 | + You can nest one callback inside in another callback to execute the actions sequentially one by one. This is known as callbacks in callbacks. Beware, too many levels of nesting lead to [Callback hell](https://github.com/ckpinguin/javascript-interview-questions/tree/master?tab=readme-ov-file#what-is-a-callback-hell) |
1687 | 1687 |
|
1688 | 1688 | ```javascript
|
1689 | 1689 | loadScript("/script1.js", function (script) {
|
|
0 commit comments