DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

1

CallBacks in JavaScript

//Call back functions -Any function that is passed as an argument to another function is known as Callback function.

function printFirstname(firstname,cb,cb2)
{

//cb recieves Lastname function
//cb2 recieves printage function
Enter fullscreen mode Exit fullscreen mode

console.log(firstname);
cb("Verma")
cb2(22);
}

function printLastname(lastname)
{
console.log(lastname);
}

function printAge(age)
{
console.log(age)
}

printFirstname("Pushan",printLastname,printAge);

//👉ans->
//Pushan
//Verma
//22

2nd example:

const fs =require('fs');

console.log("before");
// let data =fs.readFileSync('f1.txt');

fs.readFile('f1.txt',cb);
fs.readFile('f2.txt',cb2);

function cb(err,data)
{
if(err)
{
console.log(err);
}
console.log(" "+data);

}

function cb2(err,data)
{
if(err)
{
console.log(err);
}
console.log(" "+data);

}

console.log("after");

// 👉 ans ->
// before
// after
// this is file 2
// this is file 1

📌HandWritten Notes:
https://github.com/pushanverma/notes/blob/main/webd/Callbacks%20in%20Js%20.pdf

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay