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

I have a simplified function that looks like this: function(query) { myApi.exec(‘SomeCommand’, function(response) { return response; }); } Basically i want it to call myApi.exec, and return the response that is given in the callback lambda. However, the above code doesn’t work and simply returns immediately. Just for a very hackish attempt, i tried the … Read more

Create a custom callback in JavaScript

All I need to do is to execute a callback function when my current function execution ends. function LoadData() { alert(‘The data has been loaded’); //Call my callback with parameters. For example, //callback(loadedData , currentObject); } A consumer for this function should be like this: object.LoadData(success); function success(loadedData , currentObject) { //Todo: some action here … Read more

How to explain callbacks in plain english? How are they different from calling one function from another function?

How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can their power be explained to a novice programmer? 33 Answers 33 I am going to try to keep this dead simple. A “callback” is any function that is … Read more

Difference between array_map, array_walk and array_filter

What exactly is the difference between array_map, array_walk and array_filter. What I could see from documentation is that you could pass a callback function to perform an action on the supplied array. But I don’t seem to find any particular difference between them. Do they perform the same thing? Can they be used interchangeably? I … Read more

Aren’t promises just callbacks?

I’ve been developing JavaScript for a few years and I don’t understand the fuss about promises at all. It seems like all I do is change: api(function(result){ api2(function(result2){ api3(function(result3){ // do work }); }); }); Which I could use a library like async for anyway, with something like: api().then(function(result){ api2().then(function(result2){ api3().then(function(result3){ // do work }); … Read more

How to return value from an asynchronous callback function? [duplicate]

This question already has answers here: How to return the response from an asynchronous call (43 answers) Closed 7 years ago. This question is asked many times in SO. But still I can’t get stuff. I want to get some value from callback. Look at the script below for clarification. function foo(address){ // google map … Read more