-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCallbacks.js
More file actions
32 lines (29 loc) · 816 Bytes
/
Callbacks.js
File metadata and controls
32 lines (29 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function getData(callback) {
setTimeout(() => {
console.log('Step 1: Data fetched');
callback(null, 'data1');
}, 1000);
}
function processData(data, callback) {
setTimeout(() => {
console.log('Step 2: Data processed:', data);
callback(null, 'processedData1');
}, 1000);
}
function saveData(data, callback) {
setTimeout(() => {
console.log('Step 3: Data saved:', data);
callback(null, 'savedData1');
}, 1000);
}
// Nested callbacks - "Callback Hell"
getData((err, data1) => {
if (err) return console.error(err);
processData(data1, (err, processedData) => {
if (err) return console.error(err);
saveData(processedData, (err, savedData) => {
if (err) return console.error(err);
console.log('All steps completed successfully:', savedData);
});
});
});