Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4165

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T09:27:08+00:00 2024-11-26T09:27:08+00:00

JS Callback Function

  • 61k

Callback function কি ?

jS এ কোন একটি function কে অন্য আরেকটা function এর প্যারামিটার হিসেবে pass করা হয় তখন সেই function কে callback function বলা হয় ।

function anotherFunction(callback){   callback() } 
Enter fullscreen mode Exit fullscreen mode

উপরে anotherFunction নামে একটি function ডিফাইন করা হয়েছে,সেই function এর প্যারামিটারে আমরা callback নামে একটা প্যারামিটার নিয়েছি এবং সেটি anotherFuntion এর ভিতরে function call এর মতো করে call করেছি । অর্থাৎ আমরা আশা করি যে যখন ডেভেলপার anotherFunction কে invoke বা call করবে তখন anotherFunction এর argument হিসেবে একটি function কে pass করবে।

function simpleTextLog(){   console.log("simple text showing") } anotherFunction(simpleTextLog)  
Enter fullscreen mode Exit fullscreen mode

output : simple text showing

কেন Callback Function প্রয়োজন ?

মূলত asynchronous প্রোগ্রামিং এর ক্ষেত্রে callback function টি ব্যাপক ভাবে ব্যাবহার করা হয় তবুও নিচে ছোট একটি উদাহারন দিলাম।

আমরা জানি যে js এ কোড উপর থেকে নিচে লাইন বাই লাইন execute হয় । এখন যদি আমরা চাই, আমদের কোন একটা function এর কাজ শেষ হলে আরেকটি function এর কাজ শুরু হবে । সেই ক্ষেত্রে আমরা অনেক উপায়ে Function Sequence Control করতে পারি ।

যেমন : আমরা চাচ্ছি যে, দুইটি সংখ্যার যোগ ফল ডিসপ্লে করতে । সেই জন্য আমরা যোগ করার জন্য একটি function তৈরি করবো,যার কাজ হবে যোগ করা এবং ডিসপ্লে করার জন্য আরেকটি function তৈরি করবো,যার কাজ হবে শুধু ডিসপ্লে করা ।

Ex-1

function displaySum(sum){  console.log(`total sum is ${sum}`); }  function doSum(num1,num2){    const sum = num1 + num2;    return sum; }  const result = doSum(5,7) displaySum(result)  
Enter fullscreen mode Exit fullscreen mode

Ex-2

function displaySum(sum){  console.log(`total sum is ${sum}`); }  function doSum(num1,num2){    const sum = num1 + num2;   displaySum(sum) }  doSum(5,7)  
Enter fullscreen mode Exit fullscreen mode

Ex-1 এ কাজটি করার জন্য আমাদের দুটি function আলাদা করে পর পর কল করা লাগতেছে । এখানে displaySum function টি doSum function এর উপর নির্ভরশীল ছিলো । তাই এই ক্ষেত্রে এই উদাহারন কে আমরা batter Function Sequence Control বলতে পারি না ।

Ex-2 এটিও batter Function Sequence Control বলতে পারি না । কারণ ফলাফল ডিসপ্লে করার জন্য doSum function থেকে displaySum function কে control করা যাচ্ছে না । মনে করেন আপনি চাচ্ছেন যে doSum function টি আপনার আপ্লিকেশনে 3 বার কল করতে চাচ্ছেন । এখন 3 বার কল করার সময় displaySum function কল হয়ে যাবে কিন্তু আপনি চাচ্ছেন যে doSum function থেকে শুধু মাত্র একবার বা আপনার ইচ্ছা মতো displaySum function কল করবেন। সেটি করার জন্যই আমাদের callback function ব্যাবহার করতে হবে।

callback function এর মাদ্ধমে doSum function থেকে displaySum function কে control করা যাবে।
যেমন :

function displaySum(sum){  console.log(`total sum is ${sum}`); }  function doSum(num1,num2,cb){    const sum = num1 + num2;   cb && cb(sum) }  doSum(5,7,displaySum) //output: total sum is 12 doSum(5,7) //output: undefined 
Enter fullscreen mode Exit fullscreen mode

উপরের কোডে displaySum function হলো callback function যেটি doSum function এর argument হিসবে pass হয়েছে ।

*নোট : callback function হিসেবে যখন কোন function কে argument হিসেবে পাঠাবেন তখন function এর reference পাঠাতে হয় । সেই জন্য function এর name এর সাথে কখনো “()” ব্র্যাকেট বা parentheses ব্যবহার করবেন না । যখন কোন একটা function এর name এর সাথে ব্র্যাকেট বা parentheses ব্যবহার করা হয় তখন সেই function টি কল হয়ে যায় । *

callbackfunctionjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.