parent
00ba8a73c1
commit
0318fe48e3
2 changed files with 53 additions and 0 deletions
@ -0,0 +1,48 @@ |
|||||||
|
**Callback hell**, often referred to as **Pyramid of Doom**, describes a situation in JavaScript where multiple nested callbacks become difficult to manage, leading to unreadable and unmaintainable code. It often arises when performing multiple asynchronous operations that depend on the completion of previous operations. The code starts to take on a pyramidal shape due to the nesting. |
||||||
|
|
||||||
|
## Example of callback hell |
||||||
|
|
||||||
|
```js |
||||||
|
callAsync1(function () { |
||||||
|
callAsync2(function () { |
||||||
|
callAsync3(function () { |
||||||
|
callAsync4(function () { |
||||||
|
callAsync5(function () { |
||||||
|
// ... |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
|
``` |
||||||
|
|
||||||
|
## Strategies to avoid callback hell |
||||||
|
|
||||||
|
Developers can address or avoid callback hell by using strategies like modularizing the code into named functions, using asynchronous control flow libraries, or leveraging modern JavaScript features like Promises and `async/await` to write more linear, readable asynchronous code. |
||||||
|
|
||||||
|
### Promise chaining |
||||||
|
|
||||||
|
```js |
||||||
|
callAsync1() |
||||||
|
.then(() => callAsync2()) |
||||||
|
.then(() => callAsync3()) |
||||||
|
.then(() => callAsync4()) |
||||||
|
.then(() => callAsync5()) |
||||||
|
.catch((err) => console.error(err)); |
||||||
|
``` |
||||||
|
|
||||||
|
### Async/await |
||||||
|
|
||||||
|
```js |
||||||
|
async function asyncCall() { |
||||||
|
try { |
||||||
|
await callAsync1(); |
||||||
|
await callAsync2(); |
||||||
|
await callAsync3(); |
||||||
|
await callAsync4(); |
||||||
|
await callAsync5(); |
||||||
|
} catch (err) { |
||||||
|
console.error(err); |
||||||
|
} |
||||||
|
} |
||||||
|
``` |
Loading…
Reference in new issue