Trying to understand the concept when callbacks are passed as arguments

Hello guys, sorry to disturb you all; I didn't understand the importance of callbacks, like here in the following code, why use callbacks please... I'm learning about asynchronous javascript, promises and all these stuffs but I didn't understand the concept what we try to convey when we pass callback as arguments. I understand the syntax but not how they work or how we should think of them.... can someone elaborate why callback is important in the code below for e.g. and what if we don't have them and what therefore introduces the idea of promises please.
function doStep1(init, callback) {
const result = init + 1;
callback(result);
}

function doStep2(init, callback) {
const result = init + 2;
callback(result);
}

function doStep3(init, callback) {
const result = init + 3;
callback(result);
}

function doOperation() {
doStep1(0, (result1) => {
doStep2(result1, (result2) => {
doStep3(result2, (result3) => {
console.log(`result: ${result3}`);
});
});
});
}

doOperation();
function doStep1(init, callback) {
const result = init + 1;
callback(result);
}

function doStep2(init, callback) {
const result = init + 2;
callback(result);
}

function doStep3(init, callback) {
const result = init + 3;
callback(result);
}

function doOperation() {
doStep1(0, (result1) => {
doStep2(result1, (result2) => {
doStep3(result2, (result3) => {
console.log(`result: ${result3}`);
});
});
});
}

doOperation();
4 Replies
glutonium
glutonium•3w ago
i believe u made a similar post before and we talked through it actually nvm it was about call back hell
Faker
FakerOP•3w ago
yeahh, I'm still confuse though 😭 like I've understood the syntax but I didn't understand why we need them and why we should use them
glutonium
glutonium•3w ago
but ya there is a different post that goes through it https://discord.com/channels/436251713830125568/1281260269175181323 check this
Faker
FakerOP•3w ago
okkk
Want results from more Discord servers?
Add your server