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.

33 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…
7
votes
6 answers

Is a callback function with `this` as an argument a bad practice?

I have a class that has a callback function that gets triggered on some event. The user of the class instance is expected to do stuff on the instance itself, inside this callback function. So I am wondering if I should add the instance itself as an…
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…
6
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…
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…
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
  • 270
4
votes
5 answers

Is changing the signature of a callback a breaking change?

In Javascript, should appending to the signature of a callback be considered a breaking change? I.e. given an operation op(target, callback) should changing it from callback(item, index, array) to callback(item, index, array, root) result in a new…
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
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
  • 1
  • 7
3
votes
1 answer

Is there a context in oAuth, reusable in the callback?

I am developing a home-grade web application (server-client, based on Nuxt and nuxt-auth-utils). I am using the opportunity to learn something about oAuth (and OpenID). One of the issues I face is that the exchange sequence does not seem to carry…
WoJ
  • 1,661
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
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…
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…
1
2 3