The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we help you to solve your academic and programming questions fast.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

Nodejs-Interview-questions-basic-->View question

What is a call back function and what is a call back hell -Nodejs

Expalin what is a call back function and what is a call back hell. Explain it with appropriate examples and code samples


Asked On2021-09-27 03:59:37 by:sharma

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all qusetions
Answers
Callback definition is - a return call

Nodejs Is a run tie environment for java script.  Javascript is a single threaded programming language so every time a job is done it has to let know the primary thread that the job is executed successfully. 
Here is an example code for callbacks.
var fs = require('fs');
fs
.readFile('new.js',function(err,file){
if(err){
handleError(err);
}else{
console.log("file: ", file);
}
})
From the above code it is evident that  Once the file read is done then only it has to go to next process this will result a pyramid like code structure.
//Variables
var fs=require('fs');
var util =require('util');
var read =util.promisify(fs.readFile);
//Call back hell code
fs.readFile('new.js',function(err,file){
if(err){
return err;
}else{
fs.readFile('new.js',function(err,file1){
if(err){
return err;
}else{
fs.readFile('new.js',function(err,file2){
if(err){
return err;
}else{
console.log("file1 :",file);
console.log("file2 :",file1);
console.log("file3 :",file2);
}
})
}
})
}
})
To resolve this promises are being used. So the code will looks like this.
//Solving using promises
Promise.all([read('new.js'),read('new.js')])
.then(data =>{
const[data1,data2,data3]=data;
console.log('file1 :',data1);
console.log('file2 :',data2);
})

This is what we call as a call bak and a call back hell.


Answerd on:2021-09-29 Answerd By:batsam22

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Watch more here

Watch more videos from this user Here

Learn how to upload a video over here



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder