When you return something inside of a callback function, it gets returned directly from the callback of the function you executed.
For example,
1) we provide a function definition for func1’s callback parameter.
2) func1 runs its implementation and then executes the callback.
3) goes into the callback and executes code
4) we return a string inside of the callback function.
5) it gets returned from the callback inside of func1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function Foo() { } Foo.prototype.func1 = function(callback) { // 2, runs various code let b = callback("func1"); // 5 result returned to variable b. console.log(b); } let f = new Foo() // 1 let a = f.func1(function(result) { // 3, executes various code console.log(result); return result; // 4 }); console.log(a); |