Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  72] [ 3]  / answers: 1 / hits: 16154  / 6 Years ago, mon, july 30, 2018, 12:00:00

Hello Everyone hope you all doing great ,



this is my code , function with name and callback
taking the name to callback function to make return the name and console.log it



if i



function doSomething(name,callback) {
callback(name);
}

function foo(n) {
return n;
}

var val = doSomething(TEST,foo);
console.log(val);


i got undefined .



if i



function doSomething(name,callback) {
callback(name);
}

function foo(n) {
console.log(n);
}

var val = doSomething(TEST,foo);


that works fine and console.log the TEST name .



So why the return not working ?



thanks


More From » javascript

 Answers
60

Your doSomething() function doesn't return anything, which means an assignment using it will be undefined. But, that's not really the problem here.


The underlying problem is that you seem to be mixing two different data processing patterns here: if you're writing purely synchronous code, then use returning functions (which immediately return some value). If you need asynchronous code, then use a callback (which will "eventually" do something). Mixing those two patterns is a recipe for problems and frustration:


Either:



  1. don't name your function a "callback", and have it return its processed value, or

  2. make the callback responsible for doing whatever it is you were going to do with val.


Case 1:


function doSomething(data, processor) {
return processor(data);
}

function passThrough(v) { return v; }

var val = doSomething("test", passThrough);
// immediately use "val" here in for whatever thing you need to do.

Case 2:


function doSomething(data, callback) {
// _eventually_ a callback happens - for instance, this
// function pulls some data from a database, which is one
// of those inherently asynchronous tasks. Let's fake that
// with a timeout for demonstration purposes:
setTimemout(() => callback(data), 500);
}

function handleData(val) {
// use "val" here in for whatever thing you need to do. Eventually.
}

doSomething("test", handleData);

And if you want to go with case 2, you really want to have a look at "Promises" and async/await in modern Javascript, which are highly improved approaches based on the idea of "calling back once there is something to call back about".


2021 edit: a third option since original writing this answer is to use the async/await pattern, which is syntactic sugar around Promises.


Case 3:


async function doSomething(input) {
// we're still _eventually_ returning something,
// but we're now exploiting `async` to wrap a promise,
// which lets us write normal-looking code, even if what
// we're really doing is returning a Promise object,
// with the "await" keyword auto-unpacking that for us.
return someModernAsyncAPI.getThing(input);
}

function handleData(val) {
// ...
}

async function run() {
const data = await doSomething("test");
handleData(data);
}

run();

[#53848] Wednesday, July 25, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
estefanib

Total Points: 508
Total Questions: 104
Total Answers: 83

Location: Lebanon
Member since Sun, Aug 2, 2020
4 Years ago
;