keppodon
keppodon

Reputation: 85

Why do we need callback instead of calling directly inside the function? in JS

I just realized that I do not understand javascript enough, despite the fact that I have been coding it for some time.

What I am not comfortable is Javascript Async.

until now I have been using callback and promise, because documentations and tutorials did it so as below.

$.get("some/url", function() {
    // callback body
})

axios.get('some/url').then(res => {
    console.log(res.data);
});

arr.map(element => console.log(element))

I know callback is used to my our code asynchronous

function doSomething(cb) {
  console.log("this function receives callback function");
  cb();
}

but what if we just call the function inside the function manually

function func1() {
  // function body
}
function doSomething() {
  console.log("this function receives callback function");
  func1();
}

What I thought is as long as function is called at the right time, we do not have to use callback. or is it because callback does its work while some other operations are going on? If so, doesnt it break the principle of javascript as single threaded? because its doing two things at the same time.

besides, do people use promise over callback function because of its readability and you could use Promise.all() ? I feel like I am missing a core advantage of using promise.

Kindly please help me understand better.

Upvotes: 1

Views: 1677

Answers (3)

Roman  Sarder
Roman Sarder

Reputation: 99

It is all about reusability. If you call pass your callback into the utility function, in your example it is doSomething, you will not be able to reuse doSomething function in other parts of your program. You have just created a tight coupling between two functions.

If you program to the interface, and define which kind of callback you are expecting in your doSomething function, you will be able to use this doSomething function anywhere. It will not have to know which exact function to call, it will just concern about having a callback which expects n parameters in some order which.

Upvotes: 0

Stefan Dobre
Stefan Dobre

Reputation: 138

Is exactly the difference between hard coding data in your code or getting it dynamically.

In backend for example you make the code so it can serve different users different needs... at the same time, the same code...

So in this example:

function doSomething(cb) {
  console.log("this function receives callback function");
  cb();
}

you can build a functions that serve different users different information depending on the cb it receives...

in this example:

function func1() {
  // function body
}
function doSomething() {
  console.log("this function receives callback function");
  func1();
}

it just runs a specific function func1 and that's it

Upvotes: 0

Barmar
Barmar

Reputation: 781096

If you don't use a callback, then you can only call func1(). The original doSomething() is more general, since you can call different functions depending on what the caller needs.

function func1() {
    // function body
}
function func2() {
    // function body
}

doSomething(func1);
doSomething(func2);

Upvotes: 1

Related Questions