How to make a function wait until a callback has been called using node.js

The “good node.js /event driven” way of doing this is to not wait. Like almost everything else when working with event driven systems like node, your function should accept a callback parameter that will be invoked when then computation is complete. The caller should not wait for the value to be “returned” in the normal … Read more

Callback functions in C++

Note: Most of the answers cover function pointers which is one possibility to achieve “callback” logic in C++, but as of today not the most favourable one I think. What are callbacks(?) and why to use them(!) A callback is a callable (see further down) accepted by a class or function, used to customize the … Read more

How to use instance method as callback for function which takes only func or literal closure

The callback is a pointer to a C function, and in Swift you can pass only a global function or a closure (which does not capture any state), but not an instance method. So this does work: CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), nil, { (_, observer, name, _, _) in print(“received notification: \(name)”) }, “myMessage”, nil, .DeliverImmediately) But since … Read more

JavaScript: Passing parameters to a callback function

If you want something slightly more general, you can use the arguments variable like so: function tryMe(param1, param2) { alert(param1 + ” and ” + param2); } function callbackTester(callback) { callback(arguments[1], arguments[2]); } callbackTester(tryMe, “hello”, “goodbye”); But otherwise, your example works fine (arguments[0] can be used in place of callback in the tester)

I know that callback function runs asynchronously, but why?

There is nothing in the syntax that tells you your callback is executed asynchronously. Callbacks can be asynchronous, such as: setTimeout(function(){ console.log(“this is async”); }, 100); or it can be synchronous, such as: an_array.forEach(function(x){ console.log(“this is sync”); }); So, how can you know if a function will invoke the callback synchronously or asynchronously? The only … Read more

How do I run Asynchronous callbacks in Playground

While you can run a run loop manually (or, for asynchronous code that doesn’t require a run loop, use other waiting methods like dispatch semaphores), the “built-in” way we provide in playgrounds to wait for asynchronous work is to import the XCPlayground framework and set XCPlaygroundPage.currentPage.needsIndefiniteExecution = true. If this property has been set, when … Read more

Callback after all asynchronous forEach callbacks are completed

Array.forEach does not provide this nicety (oh if it would) but there are several ways to accomplish what you want: Using a simple counter function callback () { console.log(‘all done’); } var itemsProcessed = 0; [1, 2, 3].forEach((item, index, array) => { asyncFunction(item, () => { itemsProcessed++; if(itemsProcessed === array.length) { callback(); } }); }); … Read more