# Callback confusion kindly help

**URL:** <https://forum.freecodecamp.org/t/callback-confusion-kindly-help/149975>\
**Category:** General\
**Created:** [September 26, 2017, 11:47am UTC](https://forum.freecodecamp.org/t/callback-confusion-kindly-help/149975 "2017-09-26T11:47:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitshrivastavafcc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amitshrivastavafcc/32/65932_2.png) [@amitshrivastavafcc](https://forum.freecodecamp.org/u/amitshrivastavafcc)\
**Post date:** [September 26, 2017, 11:47am UTC](https://forum.freecodecamp.org/t/callback-confusion-kindly-help/149975/1 "2017-09-26T11:47:23Z")

</div>

Hi,

I was just trying use callback function and felt i understood them well , ie they are functions that are called by other function when the “other” function is done doing what it it suppose to do …

But when i run this code -

```auto

function doCallBack(callback) {
    setTimeout(function(){
       console.log("Doing work");
    },3000);
   
    callback();
}

doCallBack(function () {
   console.log("Done waiting for 3 seconds"); 
});

```

I find despite the setTimeout for 3 seconds the output is Done waiting 3 seconds and then Doing work …

Kindly guide why is the callback getting called first rather then waiting for 3 seconds … Thanks

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [September 26, 2017, 11:59am UTC](https://forum.freecodecamp.org/t/callback-confusion-kindly-help/149975/2 "2017-09-26T11:59:11Z")

</div>

What happens roughly when you call `doCallBack` is

1. The `setTimeout` function is called. It sets the function that you pass it to run 3 seconds after there’s nothing else in your code to run.
2. The `callback` function is called. The console prints `'Done waiting for 3 seconds'`.
3. There’s nothing else to be done in your code. At this point, the timeout that you set kicks in after 3 seconds.
4. 3 seconds later, the console prints `'Doing work'`.

`setTimeout` doesn’t pause execution of code. Rather it schedules code that you pass it to run some time later after your code runs. This scheduling doesn’t take too much time, and immediately after that the code after it runs.

---

<div class="post-metadata">

**Author:** ![amitshrivastavafcc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/amitshrivastavafcc/32/65932_2.png) [@amitshrivastavafcc](https://forum.freecodecamp.org/u/amitshrivastavafcc)\
**Post date:** [September 26, 2017, 12:16pm UTC](https://forum.freecodecamp.org/t/callback-confusion-kindly-help/149975/3 "2017-09-26T12:16:36Z")

</div>

Thank you so much i tried this below and seems to work as expected -

```auto

function doCallBack(callback) {
    var ms = 3000 + new Date().getTime();
    while(ms > new Date().getTime()){
        
    }
    callback();
}

function callback() {
    console.log("Done waiting for 3 seconds"); 
}

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:39pm UTC](https://forum.freecodecamp.org/t/callback-confusion-kindly-help/149975/4 "2021-01-17T20:39:03Z")

</div>

Closed on January 17, 2021, 8:39pm UTC


