What is a “callback” in C and how are they implemented?

There is no “callback” in C – not more than any other generic programming concept. They’re implemented using function pointers. Here’s an example: void populate_array(int *array, size_t arraySize, int (*getNextValue)(void)) { for (size_t i=0; i<arraySize; i++) array[i] = getNextValue(); } int getNextRandomValue(void) { return rand(); } int main(void) { int myarray[10]; populate_array(myarray, 10, getNextRandomValue); … … Read more

Callback to a Fragment from a DialogFragment

Activity involved is completely unaware of the DialogFragment. Fragment class: public class MyFragment extends Fragment { int mStackLevel = 0; public static final int DIALOG_FRAGMENT = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { mStackLevel = savedInstanceState.getInt(“level”); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(“level”, mStackLevel); } void … Read more

nodeJs callbacks simple example

var myCallback = function(data) { console.log(‘got data: ‘+data); }; var usingItNow = function(callback) { callback(‘get it?’); }; Now open node or browser console and paste the above definitions. Finally use it with this next line: usingItNow(myCallback); With Respect to the Node-Style Error Conventions Costa asked what this would look like if we were to honor … Read more

How to “await” for a callback to return?

async/await is not magic. An async function is a function that can unwrap Promises for you, so you’ll need api.on() to return a Promise for that to work. Something like this: function apiOn(event) { return new Promise(resolve => { api.on(event, response => resolve(response)); }); } Then async function test() { return await apiOn( ‘someEvent’ ); … Read more

Create a custom callback in JavaScript

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 + ” ” + … Read more

How should I call 3 functions in order to execute them one after the other?

In Javascript, there are synchronous and asynchronous functions. Synchronous Functions Most functions in Javascript are synchronous. If you were to call several synchronous functions in a row doSomething(); doSomethingElse(); doSomethingUsefulThisTime(); they will execute in order. doSomethingElse will not start until doSomething has completed. doSomethingUsefulThisTime, in turn, will not start until doSomethingElse has completed. Asynchronous Functions … Read more

C++ callback using class member

Instead of having static methods and passing around a pointer to the class instance, you could use functionality in the new C++11 standard: std::function and std::bind: #include <functional> class EventHandler { public: void addHandler(std::function<void(int)> callback) { cout << “Handler added…” << endl; // Let’s pretend an event just occured callback(1); } }; The addHandler method … Read more