Table of contents
No headings in the article.
Hi There,
From now on, I want to add some questions and their answers which I encountered during my endless interviews 👀. I am pretty sure, more or less you will face these questions as well. I hope it will help someone else out there. There can be different solutions to each question and if you think you have different/better/elegant ...etc, please do not hesitate to share them with me. I am planning to publish these articles as a javascript interview series. Stay tuned.
Without further ado Let's start⭐️
What is a
callback
function. What are the disadvantages of using them?
As its name indicates, a
callback function
is nothing more than a function that is called after specific operations.For example, After signing in to a website, this website can show you a welcoming message. We can think of this scenario in code like below 💭
const signInToMyAwsomeSite = (args,greetingCallback) => {
//some checks , operations
const [error,result] = ...; //error, result of checking some logic
return error ? greetingCallback(null,error) : greetingCallback(result,null);
}
- In the above code, After signing in to a website, If an error occurs we will
execute/call
ourcallback
function with...(null, error)
and otherwise with...(data, null)
** As we saw that, what is wrong with callbacks
? What are the cons?**
If both the main function
and callback
functions are our responsibility then there would be fewer disadvantages. Even in this case, we will have to tackle it with callback hells
. But let's talk about a different scenario.
- Let's assume that we are doing a hotel reservation system. Our team is responsible for checking user inputs and some other business logic checks. But the payment is handled by a
third party
system. We can imagine our code block as below👇
import {paymentCallback} from './thirdParty/services/payment';
const reserveHotel = (args,paymentCallback) => {
//make some checks for user inputs and relevant business logic
const creditCardDetails = {
//... user credit card details
}
paymentCallback(creditCardDetails);
}
At first, everything might seem ok. But think about it. This
creditCardDetails
payment method is out of our control. We can not call the payment directly. It is another team's responsibility. But here are some questions that arise after some thinking. 🤔 Will they be able to call it as expected? Will they call it less/more than expected? What if an error occurred in their system, what is the behavior? Will they call the payment again or not? The list goes on and on ...As we can see from the endless list there are lots of things going on out of our control. We can not trust other systems💯 .
So what should we do in such a scenario? Luckily we have
Promises
it in javascript. You can read about promises in one of my previous articles.
Write a fibonacci function that takes a number(n) and returns n of the fibonacci numbers. First write imperative version than recursive and finally iterator version of that function
//Write imperative version of fibonacci(n)
//Output: fibonacci(10) => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
const fibonacciIterative = (n) => {
if (n === 1) {
return [0]
}
if (n === 2) {
return [0,1]
}
const result = [0,1]
const length = result.length
while (result.length < n) {
result.push(result[result.length - 2] + result[result.length - 1]);
}
return result
}
//fibonacciIterative(10) => => [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
//----------------------------------------------------
const fibonacciRecursive = (n) => {
if (n === 1) {
return [0]
}
if (n === 2) {
return [0,1]
}
const result = fibonacciRecursive(n - 1);
result.push(result.at(-1) + result.at(-2))
return result;
}
//fibonacciRecursive(5) => [0, 1, 1, 2, 3]
//----------------------------------------------------
function* fibonacciIterator() {
let [ a, b ] = [ 0, 1]
while (true) {
yield a;
[a, b] = [b, a + b]
}
}
let i = 0;
const result = []
for (const fib of fibonacciIterator()) {
if (i++ === 10) {
break
}
result.push(fib)
}
console.log(result)
/*
const result = []
const iterator = fibonacciIterator();
let i = 0;
while (i++ < 10) {
result.push(iterator.next().value)
}
console.log(result)
*/
I will talk about how to promisfy
a callback function in the upcoming article. By converting our ready callback functions to promise counterparts we will leverage promises' benefits.
Stay positive/healthy, bye-bye. ✌️