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

You must login to ask a question.

Forgot Password?

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 586417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:06:18+00:00 2026-05-13T15:06:18+00:00

All I need to do is to execute a callback function when my current

  • 0

All I need to do is to execute a callback function when my current function execution ends.

function LoadData() 
{
    alert('The data has been loaded');
    //Call my callback with parameters. For example,
    //callback(loadedData , currentObject);
}

A consumer for this function should be like this:

object.LoadData(success);

function success(loadedData , currentObject) 
{
  //Todo: some action here 
}

How do I implement this?

  • 1 1 Answer
  • 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

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:06:19+00:00Added an answer on May 13, 2026 at 3:06 pm

    Actually, your code will pretty much work as is, just declare your callback as an argument and you can call it directly using the argument name.

    The basics

    function doSomething(callback) {
        // ...
    
        // Call the callback
        callback('stuff', 'goes', 'here');
    }
    
    function foo(a, b, c) {
        // I'm the callback
        alert(a + " " + b + " " + c);
    }
    
    doSomething(foo);
    

    That will call doSomething, which will call foo, which will alert “stuff goes here”.

    Note that it’s very important to pass the function reference (foo), rather than calling the function and passing its result (foo()). In your question, you do it properly, but it’s just worth pointing out because it’s a common error.

    More advanced stuff

    Sometimes you want to call the callback so it sees a specific value for this. You can easily do that with the JavaScript call function:

    function Thing(name) {
        this.name = name;
    }
    Thing.prototype.doSomething = function(callback) {
        // Call our callback, but using our own instance as the context
        callback.call(this);
    }
    
    function foo() {
        alert(this.name);
    }
    
    var t = new Thing('Joe');
    t.doSomething(foo);  // Alerts "Joe" via `foo`
    

    You can also pass arguments:

    function Thing(name) {
        this.name = name;
    }
    Thing.prototype.doSomething = function(callback, salutation) {
        // Call our callback, but using our own instance as the context
        callback.call(this, salutation);
    }
    
    function foo(salutation) {
        alert(salutation + " " + this.name);
    }
    
    var t = new Thing('Joe');
    t.doSomething(foo, 'Hi');  // Alerts "Hi Joe" via `foo`
    

    Sometimes it’s useful to pass the arguments you want to give the callback as an array, rather than individually. You can use apply to do that:

    function Thing(name) {
        this.name = name;
    }
    Thing.prototype.doSomething = function(callback) {
        // Call our callback, but using our own instance as the context
        callback.apply(this, ['Hi', 3, 2, 1]);
    }
    
    function foo(salutation, three, two, one) {
        alert(salutation + " " + this.name + " - " + three + " " + two + " " + one);
    }
    
    var t = new Thing('Joe');
    t.doSomething(foo);  // Alerts "Hi Joe - 3 2 1" via `foo`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So all I need is a simple function that sets it up (eating incoming
I need to execute a bunch of asynchronous methods (client SQLite database), and call
I need a callback to execute once after multiple elements have finished animating. My
I need to execute stored procedure sp_spaceused for all the tables in my database.
I need to execute a specific function mvFinishItUp() when two conditions are met. More
I have multiple threads who all need to write to the same Dictionary. I
I have a whole bunch of buttons that all need to have both a
I just have the array of 15 values that all need to be inserted
Alright simple question I have integriters like 5 188 4634 And they all need
All I need this for is strcpy(). I want to see whether the first

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.