Javascript Callbacks — How do they work ?

JS callbacks
const getNumber = () => { 
return 10
}
console.log(getNumber())
[thejsway@jsmachine]$ node callback.js
10
const getNumber = () => { 
setTimeout(() => {
return 10
},3000)
}
console.log(getNumber())
[thejsway@jsmachine]$ node callback.js
undefined
const getNumber = (cb) => { 
setTimeout(() => {
cb(10)
},3000)
}
// Passed a function in getNumber() function as a parameter
getNumber((output) => {
console.log(output)
})
  1. We added a new parameter while calling getNumber() function and also added a parameter in getNumber() function i.e. cb.
  2. When we call getNumber() in the above format, a function is passed and is assigned to cb parameter.
  3. After setTimeout() time i.e. 3 seconds, is over, it will run the function and inside that, it will call the cb function and passes an integer 10 as its parameters.
  4. Now the function where output is logged in console will be called.
[thejsway@jsmachine]$ node callback.js
10
  1. We will make this function asyncronous by using setTimeout() function and check if the number passed is even or not after two seconds.
  2. If the number is even, pass true in callback and if the number is odd, pass false. If no number is passed, pass error message in callback.
  3. We will write a callback function which will take two arguments, first is isEven and second is error message.
const isEven = (number, cb) => { 
setTimeout(() => {
if(!number) {
return cb(undefined, "No number is passed")
}
if(number % 2 === 0) {
cb(true, undefined)
} else {
cb(false, undefined)
}
}, 2000)
}
// Passed 10 to check if it is even or not
isEven(10, (result, error) => {
if(error){
console.log("10:" + error)
}
else {
console.log("10: " + result)
}
})
// Passed 11 to check if it is even or not
isEven(11, (result, error) => {
if(error){
console.log("11: " + error)
}
else {
console.log("11: " + result)
}
})
// Passed undefined to check if it is even or not
isEven(undefined, (result, error) => {
if(error){
console.log("undefined: " + error)
}
else {
console.log("undefined: " + result)
}
})
const isEven = (number, cb) => { 
setTimeout(() => {
if(!number) {
return cb(undefined, "No number is passed")
}
if(number % 2 === 0) {
cb(true, undefined)
} else {
cb(false, undefined)
}
}, 2000)
}
// Passed 10 to check if it is even or not
isEven(10, (result, error) => {
if(error){
console.log("10:" + error)
}
else {
console.log("10: " + result)
}
})
// Passed 11 to check if it is even or not
isEven(11, (result, error) => {
if(error){
console.log("11: " + error)
}
else {
console.log("11: " + result)
}
})
// Passed undefined to check if it is even or not
isEven(undefined, (result, error) => {
if(error){
console.log("undefined: " + error)
}
else {
console.log("undefined: " + result)
}
})
[thejsway@jsmachine]$ node callback.js
10: true
11: false
undefined: No number is passed

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store