---
# System prepended metadata

title: Untitled

---

CallBack Intro 

15-20
So a function could takes input parameters and returns an output, lets take an example:

```
function sum(n1,n2){
    return n1+n2;
}
```

That's pretty simple right, takes input and return an output. 


Let's get more advance what a function takes in a parameter another function 
```javascript=
function zero(){
    return 0;
}

function one(){
    return 1;
}

function myFunc(x, f1, f2){
    if(x > 5){
        return f1();
    } else{
        return f2();
    }
}

// Call function myFunc:
myFunc(8, zero, one);

```

You have passed functions as a parameter before:

```javascript=
test('description of the test', function(t){

});

Could be:

function testFunc(t){
    // Create t.assert...
}

test('description of the test', testFunc);

```

Another example of callback usage:
[MUHAMMAD CHANGE END]


```javascript=
function delayTenSecond(callback){
    setTimeout(callback,1000*10)
}
```

So in a nutshell a callback is function is a given upon as a parameter and will be executed at somepoint in time. 

That somepoint in time may refer to 

1. Timer 
2. A DOM Event 
3. Anything that needs time to be ready to be executed, could you thing of somthing ? 



This brings to an important topic in JavaScript which Sync/Async 

It's important to know that JavaScript is a single threaded language, in other words JavaScript can't do two things in a time, it appears to do so but isn't. 

The waiter analogey is that a waiters go to a table ask people what they want to order and then he wont block himself until their order is ready. 

So does do JavaScript if an opreation depends on other factor to be exectued like a timer or a request to fetch data, it won't stop executing for it 



15-20

Ask them about passing a function as  a callback where the function have to get parameters which will force them to create an anonmoyous function 





10-20


Talk about how to take the outpue of a function that is not Sync, consider the scinario when you want the output of a function  to equal a variable 

