The effect of huma
1. Field of the In
Introduction {#Sec
Q: Converting to
#ifndef Z_OBJ_WIRE
When I was about t
A large number of
I’ve been working
The present invent
The effect of a 3-

Celebrate St. Patr
The influence of n
SALT LAKE CITY — A
It has been sugges
Cochlear implant s
The present invent
The long term obje
Gift Baskets Freq
All relevant data
A Few Good Reaso
Q: how to return the contents of a function called in a callback function How do I return the contents of the function call to the caller? When I try to use return, nothing happens, it goes back to the previous function without returning any content. I want to return the value of the last function call in this line: console.log(this.callback()); this.mainFunction(this.state); this.callback = function(){ return this.callback('this is a string'); } this.callback(); function(arg){ return this.callback('another string: ' + arg); } A: You can't. That's not how this works in JavaScript. Instead, make your callback (or rather the anonymous function that you use to construct it) an argument and make the calling function accept the return value: function callMyFunction (callback) { callback('arg'); callback('another arg'); callback('and another'); } callMyFunction(function (arg) { return arg + 'is'; }); I'm not sure if it works exactly like that, but that's what I mean. The function object in JavaScript is pretty magical: all references to functions in the same scope will always refer to the same object. So the value that you get from the last invocation is the value that all future invocations will get as well. A: you can't. The this value is set by how the function is called. If you call it with (), you will get its return value. If you call it with new, you get a new object. But it's not magically transfered back to the outer function. A: Here's a solution: function(arg){ return new f.fn(function(){ this.callback('another string: ' + arg); }); } where f.fn is: function f(f) { var that = {}; f.fn = function(callback) { return that; }; that.callback = function(string) { return function(arg) { console.log(string + ':' + arg); }; }; return that; } so that this function: var newObj = f(this.callback); newObj("hello"); newObj("world"); will work just like this function: function(callback) { callback("hello"); callback("world"); } It's actually using JavaScript's new, which you can read about on Wikipedia. http://en.wikipedia.org/wiki/New_(keyword) The reason your previous example does not work is that the IIFE returns an object. You can see that in the following code snippet: var obj = (function() { console.log("this is inside the inner function"); return 42; })(); console.log(obj); If you use a browser like Firebug, you can see that it logs this is inside the inner function then 42. Then the second console.log never happens. I want to return the value of the last function call in this line: console.log(this.callback()); You should never have to write this code in the first place because in most cases, you should just use promises to pass back values or pass callbacks. But if you must do it in this way, you can do it like this. I've tested the code on Google Chrome and using Chrome's inspector I get what I expect. var f = function(callback) { var that = {}; var promise = new Promise(function(resolve, reject) { that.callback = function(string) { console.log(string + ':' + arguments[0]); resolve(arguments[0]); }; }); that.callback = function(string) { return function(arg) { console.log(string + ':' + arg); }; }; return that; }; this.mainFunction(this.state); this.callback = function(arg) { return this.callback('hello'); }; this.callback = function(arg) { return this.callback('world'); }; var newObj = f(this.callback); newObj("hello"); newObj("world"); And to make it a bit more understandable: function f(callback) { var that = {}; var promise = new Promise(function(resolve, reject) { that.callback = function(string) { console.log(string + ':' + arguments[0]); resolve(arguments[0]); }; }); that.callback = function(string) { return function(arg) { console.log(string + ':' + arg); }; }; return that; }; var newObj = f(this.callback); newObj("hello"); newObj("world"); A: You may want to use bind which sets the value of this for the callback function. this.mainFunction(this.state); this.callback = function() { return this.callback.bind(this, 'hello')(); }; this.callback();