user2520410
user2520410

Reputation: 479

What does callback stand for?

I'm new to JS. I'm studying the following code and I'm wondering what the word callback stands for, what does it?

function(fileID, callback) {
  var request = $.ajax({
    url: url + "wishlist/" + fileID,
    type: "GET"
  });

  request.done(callback);

  request.fail(function (jqXHR, textStatus) {

  }); 
}

Upvotes: 2

Views: 298

Answers (2)

Quentin
Quentin

Reputation: 943230

It doesn't stand for anything, it is not an abbreviation.

In general terms, a callback is a piece of code which is passed to a function to be executed at a later time.

In JavaScript, that "piece of code" is almost always a function.

The simplest example of such is setTimeout where the first argument is the code (which should be a function but may be a string to be evalulated) and the second argument is the number of milliseconds from now that the "later time" occurs at.

function aFunction () { alert("Ta da"); }
setTimeout( aFunction /* The callback function */,
            500 /* 500ms from now */ 
          );

In the example you give in the question, the "later time" is "When the response to the HTTP request has been received". Another example might be "When the user clicks on a link":

document.querySelector('a').addEventListener('click', aFunction);

See also: the Wikipedia entry on callbacks.

Upvotes: 2

Andres
Andres

Reputation: 10717

Callback is the function invoked when the response returns from the server.

Upvotes: 4

Related Questions