when should callback functions in JavaScript be used












-1















can someone explain me how should we know that when we should use a callback?
like in the code given here as a link
snip of code is given here
we see that in readFile method inside fetchAll(cb), we used callback denoted by (cb) to read the content, parse it and stringify it and whatever, but in readFile method of save(), there was no need to use (cb). So how can we know when to use the callback?










share|improve this question




















  • 2





    All relevant code need to be posted here directly as text. And you use a callback when you have code to run after since action, and you aren't sure when the action will complete.

    – Carcigenicate
    Nov 23 '18 at 12:58
















-1















can someone explain me how should we know that when we should use a callback?
like in the code given here as a link
snip of code is given here
we see that in readFile method inside fetchAll(cb), we used callback denoted by (cb) to read the content, parse it and stringify it and whatever, but in readFile method of save(), there was no need to use (cb). So how can we know when to use the callback?










share|improve this question




















  • 2





    All relevant code need to be posted here directly as text. And you use a callback when you have code to run after since action, and you aren't sure when the action will complete.

    – Carcigenicate
    Nov 23 '18 at 12:58














-1












-1








-1








can someone explain me how should we know that when we should use a callback?
like in the code given here as a link
snip of code is given here
we see that in readFile method inside fetchAll(cb), we used callback denoted by (cb) to read the content, parse it and stringify it and whatever, but in readFile method of save(), there was no need to use (cb). So how can we know when to use the callback?










share|improve this question
















can someone explain me how should we know that when we should use a callback?
like in the code given here as a link
snip of code is given here
we see that in readFile method inside fetchAll(cb), we used callback denoted by (cb) to read the content, parse it and stringify it and whatever, but in readFile method of save(), there was no need to use (cb). So how can we know when to use the callback?







node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:36









Luke Walker

27314




27314










asked Nov 23 '18 at 12:53









Ratnabh kumar raiRatnabh kumar rai

11




11








  • 2





    All relevant code need to be posted here directly as text. And you use a callback when you have code to run after since action, and you aren't sure when the action will complete.

    – Carcigenicate
    Nov 23 '18 at 12:58














  • 2





    All relevant code need to be posted here directly as text. And you use a callback when you have code to run after since action, and you aren't sure when the action will complete.

    – Carcigenicate
    Nov 23 '18 at 12:58








2




2





All relevant code need to be posted here directly as text. And you use a callback when you have code to run after since action, and you aren't sure when the action will complete.

– Carcigenicate
Nov 23 '18 at 12:58





All relevant code need to be posted here directly as text. And you use a callback when you have code to run after since action, and you aren't sure when the action will complete.

– Carcigenicate
Nov 23 '18 at 12:58












2 Answers
2






active

oldest

votes


















0














Its simple. just be aware what is the nature of the methods you are using.
readFile is async so it will need a callback. What it is basically saying is "hey, I am going to read the file you asked, but while I read it you can do other stuff instead of waiting for me and when I am finished, I will come back to you". In your readFile method in save() you still pass a callback:



(err, fileContent) => {
// do stuff
}


Callback are used to handle async code so we can continue work, while something else is happening and we do not want to stop and wait for it.






share|improve this answer
























  • but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

    – Ratnabh kumar rai
    Nov 23 '18 at 21:39













  • Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

    – squeekyDave
    Nov 23 '18 at 21:55



















0














const fs=require('fs')
const path=require('path')
module.exports=class Prroduct{
constructor(title,imgurl,description,price){
this.title=title
this.imgurl=imgurl
this.description=description
this.price=price
}
save(){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
let products=
if(!err){
products=JSON.parse(fileContent)
}
products.push(this)
fs.writeFile(p,JSON.stringify(products),(err)=>{
console.log(err)
})
})
}
static fetchAll(cb){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
if(err){
cb()
}
cb(JSON.parse(fileContent))
})
}
}





share|improve this answer
























  • @squeekyDave here is the code

    – Ratnabh kumar rai
    Nov 25 '18 at 6:55











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447103%2fwhen-should-callback-functions-in-javascript-be-used%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Its simple. just be aware what is the nature of the methods you are using.
readFile is async so it will need a callback. What it is basically saying is "hey, I am going to read the file you asked, but while I read it you can do other stuff instead of waiting for me and when I am finished, I will come back to you". In your readFile method in save() you still pass a callback:



(err, fileContent) => {
// do stuff
}


Callback are used to handle async code so we can continue work, while something else is happening and we do not want to stop and wait for it.






share|improve this answer
























  • but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

    – Ratnabh kumar rai
    Nov 23 '18 at 21:39













  • Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

    – squeekyDave
    Nov 23 '18 at 21:55
















0














Its simple. just be aware what is the nature of the methods you are using.
readFile is async so it will need a callback. What it is basically saying is "hey, I am going to read the file you asked, but while I read it you can do other stuff instead of waiting for me and when I am finished, I will come back to you". In your readFile method in save() you still pass a callback:



(err, fileContent) => {
// do stuff
}


Callback are used to handle async code so we can continue work, while something else is happening and we do not want to stop and wait for it.






share|improve this answer
























  • but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

    – Ratnabh kumar rai
    Nov 23 '18 at 21:39













  • Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

    – squeekyDave
    Nov 23 '18 at 21:55














0












0








0







Its simple. just be aware what is the nature of the methods you are using.
readFile is async so it will need a callback. What it is basically saying is "hey, I am going to read the file you asked, but while I read it you can do other stuff instead of waiting for me and when I am finished, I will come back to you". In your readFile method in save() you still pass a callback:



(err, fileContent) => {
// do stuff
}


Callback are used to handle async code so we can continue work, while something else is happening and we do not want to stop and wait for it.






share|improve this answer













Its simple. just be aware what is the nature of the methods you are using.
readFile is async so it will need a callback. What it is basically saying is "hey, I am going to read the file you asked, but while I read it you can do other stuff instead of waiting for me and when I am finished, I will come back to you". In your readFile method in save() you still pass a callback:



(err, fileContent) => {
// do stuff
}


Callback are used to handle async code so we can continue work, while something else is happening and we do not want to stop and wait for it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 14:37









squeekyDavesqueekyDave

383114




383114













  • but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

    – Ratnabh kumar rai
    Nov 23 '18 at 21:39













  • Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

    – squeekyDave
    Nov 23 '18 at 21:55



















  • but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

    – Ratnabh kumar rai
    Nov 23 '18 at 21:39













  • Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

    – squeekyDave
    Nov 23 '18 at 21:55

















but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

– Ratnabh kumar rai
Nov 23 '18 at 21:39







but in my readFIle method in fetchAll() also i passed callback like (err, fileContent) so why i used there cb ? what was its need? cant it be done without cb as it is done inside save()?

– Ratnabh kumar rai
Nov 23 '18 at 21:39















Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

– squeekyDave
Nov 23 '18 at 21:55





Post all relevant code, posting an image as external source is a bad practice. Post the code where you call fetchAll, because you are passing a function whwn you call it and I cannot see any of that in the picture.

– squeekyDave
Nov 23 '18 at 21:55













0














const fs=require('fs')
const path=require('path')
module.exports=class Prroduct{
constructor(title,imgurl,description,price){
this.title=title
this.imgurl=imgurl
this.description=description
this.price=price
}
save(){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
let products=
if(!err){
products=JSON.parse(fileContent)
}
products.push(this)
fs.writeFile(p,JSON.stringify(products),(err)=>{
console.log(err)
})
})
}
static fetchAll(cb){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
if(err){
cb()
}
cb(JSON.parse(fileContent))
})
}
}





share|improve this answer
























  • @squeekyDave here is the code

    – Ratnabh kumar rai
    Nov 25 '18 at 6:55
















0














const fs=require('fs')
const path=require('path')
module.exports=class Prroduct{
constructor(title,imgurl,description,price){
this.title=title
this.imgurl=imgurl
this.description=description
this.price=price
}
save(){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
let products=
if(!err){
products=JSON.parse(fileContent)
}
products.push(this)
fs.writeFile(p,JSON.stringify(products),(err)=>{
console.log(err)
})
})
}
static fetchAll(cb){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
if(err){
cb()
}
cb(JSON.parse(fileContent))
})
}
}





share|improve this answer
























  • @squeekyDave here is the code

    – Ratnabh kumar rai
    Nov 25 '18 at 6:55














0












0








0







const fs=require('fs')
const path=require('path')
module.exports=class Prroduct{
constructor(title,imgurl,description,price){
this.title=title
this.imgurl=imgurl
this.description=description
this.price=price
}
save(){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
let products=
if(!err){
products=JSON.parse(fileContent)
}
products.push(this)
fs.writeFile(p,JSON.stringify(products),(err)=>{
console.log(err)
})
})
}
static fetchAll(cb){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
if(err){
cb()
}
cb(JSON.parse(fileContent))
})
}
}





share|improve this answer













const fs=require('fs')
const path=require('path')
module.exports=class Prroduct{
constructor(title,imgurl,description,price){
this.title=title
this.imgurl=imgurl
this.description=description
this.price=price
}
save(){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
let products=
if(!err){
products=JSON.parse(fileContent)
}
products.push(this)
fs.writeFile(p,JSON.stringify(products),(err)=>{
console.log(err)
})
})
}
static fetchAll(cb){
const p=path.join(__dirname,'../','data','products.json')
fs.readFile(p,(err,fileContent)=>{
if(err){
cb()
}
cb(JSON.parse(fileContent))
})
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 6:53









Ratnabh kumar raiRatnabh kumar rai

11




11













  • @squeekyDave here is the code

    – Ratnabh kumar rai
    Nov 25 '18 at 6:55



















  • @squeekyDave here is the code

    – Ratnabh kumar rai
    Nov 25 '18 at 6:55

















@squeekyDave here is the code

– Ratnabh kumar rai
Nov 25 '18 at 6:55





@squeekyDave here is the code

– Ratnabh kumar rai
Nov 25 '18 at 6:55


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447103%2fwhen-should-callback-functions-in-javascript-be-used%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Fiat S.p.A.