Questions tagged [callbacks]

A callback is typically the address of a function or method or lambda expression provided when invoking an API. When an action completes, the expression is executed or "called back". Asynchronous APIs use callbacks to notify the calling function when an operation is complete. Use this tag for questions involving asynchronous APIs requiring a call back expression. Use the tag event-handling for questions on registering or subscribing for events, e.g. GUI.

A callback argument is the address of a function or method or lambda expression which is executed when the function that receives the callback argument completes some action.

A callback argument is normally used with asynchronous functions or APIs in which a function is called in order to start some action which may take a while to complete. Examples of this kind of an action are:

  • writing to a disk file or other storage,
  • accessing a REST web service to request data, and
  • displaying a dialog which displays information which must be collected at the time the dialog is displayed such as a print dialog which shows which printers are available on the local network.

In the above examples, the action requested may take several milliseconds to several seconds from when it is initiated to when it completes. The use of a callback argument allows the function to be called in order to start the action knowing that when the action is complete the call back will be invoked for any final functionality needed by the caller once the action is complete.

These other actions may be happening in some other thread or some other process or some other device however the API provides some guarantee that when the action completes or it times out, the call back will be called to let you know the final results of the action.

See also:

The primary difference between an event handler and a callback is that a callback is typically a one shot deal in which an action is started, the action completes, the callback is invoked.[citation needed]

An event handler is a function or method or lambda expression which is provided as part of subscribing to one or more events. The idea of an event handler is that it may be called repeatedly as the events happen and the event handler may be one of several event handlers chained together and all subscribing to the same event. An example would be an event handler for button events or mouse events in a GUI.

29 questions
99
votes
1 answer

Is there really a fundamental difference between callbacks and Promises?

When doing single-threaded asynchronous programming, there are two main techniques that I'm familiar with. The most common one is using callbacks. That means passing to the function that acts asynchronously a callback-function as a parameter. When…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
7
votes
2 answers

Ensure that callbacks registration and triggering don't cause infinite recursion

I just spent a long, miserable week debugging a stack-overflow in a C++/Qt application. The fundamental problem was that I had a function that accepted a callback, and in certain cases the callback was triggered (in another function) before the…
6
votes
1 answer

What is a "tear-off" in software design or patterns?

What is a tear-off? I ran across the term reading Flutter documentation: Returns a CallbackHandle that can be provided to PluginUtilities.getCallbackFromHandle to retrieve a tear-off of the original callback. Googling the term only seems to…
Pete Alvin
  • 179
  • 6
5
votes
4 answers

How do I know if my async function is truly asynchronous?

I'm writing a function in the node.js-style asynchronous way, but how do I know if these functions are truly asynchronous, i.e., that they run in parallel and not sequentially? Here is my code (modified to remove details): // write the async…
chharvey
  • 264
  • 2
  • 13
5
votes
2 answers

Library IO: Use Interface classes or Callbacks?

I'm building a smallish library (few kLOC) which processes stream data in C++. From the streamed data (comes in packets) the library compiles a database piece by piece and naturally has to convey that information back to the host application. Since…
Jan Krüger
  • 53
  • 1
  • 4
5
votes
2 answers

How should a service communicate with an activity in real-time?

How should a service communicate with an activity in real-time? BroadcastReceivers seem too slow and unreliable. Sometimes they appear too slow or stop working entirely. As an example, there was a delay of a few minutes between sending and receiving…
David
  • 271
  • 1
  • 2
  • 11
4
votes
0 answers

How to authenticate third-party callbacks that don't support authentication

I'm working on a project that integrates with a third-party service via API, and the third-party uses callbacks to update us on the status of the operations being performed. These callbacks can be hit up to a day after the original method call as…
4
votes
1 answer

Scala Callback Pyramid of Doom

I would like to solicit some general design principles and best practices to avoid creating a callback pyramid of doom particularly in the Scala language. Consider the following rudimentary and imaginary code snippet: val response:…
Coder Guy
  • 727
  • 1
  • 5
  • 7
4
votes
1 answer

Designing a flexible API with support for Callbacks

I am writing a Java library that needs to make http GET && POST requests. There will be two types of users of this library: Those with understanding of callbacks and know how to use them. Those that....don't. So please consider the following two…
Joel Min
  • 143
  • 7
3
votes
2 answers

Callback pattern - return value confusion

I have a ConstraintsResolver class which resolves a queue of Constraints. This is done when a ConstraintsResolver object calls meetConstraint() on a Constraint one. Most of meetConstraint() implementations will return immediately, so I could just…
Themelis
  • 139
  • 3
3
votes
2 answers

When to use Future<> vs Listener

I've started working on an existing project (an sdk) at work, and the code base uses listeners like there's no tomorrow. Pretty much every second method takes some sort of listener argument and I have yet to find an occurrence where the listener is…
Alex Meuer
  • 141
  • 1
  • 8
3
votes
2 answers

Publish / Subscribe via HTTP Callbacks?

My team is tasked with creating a publish/subscribe system for incoming REST messages. 99% of the time, this system will be used for notifications between different processes on the same cpu, but we will need to support notification over the…
2
votes
1 answer

For a use once only button callback, should the name be more related to the button or the task of the callback?

Suppose I have UI like it: Message:

Which a…
ocomfd
  • 5,652
  • 8
  • 29
  • 37
2
votes
1 answer

Is it better to dispatch one event or multiple events for remote calls?

In ActionScript the URLLoader class that has seven separate events than can occur after you call the load() method. In the HTTPServive classes there are two events, fault and result. In one of the classes I've wrote there is only one event, result.…
1.21 gigawatts
  • 1,209
  • 1
  • 10
  • 22
2
votes
1 answer

How to clarify the control flow when dealing with many callbacks?

Traditional programming could be done quite readable. Like this: FUNCTION do_HTTP_request(url) { if(!ask_user_if_he_wants_to_connect()) return; if(!network_is_enabled()){ enable_network(); } var content = download(url); …
1
2