I am trying to understand the callback concept.
When I do that:
var myCallback = function(data) {
console.log( 'got data: '+data);
};
var usingItNow = function(callback) {
callback('get it?');
};
usingItNow(myCallback);
I get that as output as expected:
got data: get it?
But when I do the below, I get undefined
as output, why is that?
var myCallback = function(data) {
return 'got data: '+data;
};
var usingItNow = function(callback) {
callback('get it?');
};
console.log(usingItNow(myCallback));
You need to return from the usingItNow
as well.
const myCallback = function(data) {
return 'got data: '+ data;
};
const usingItNow = function(callback) {
return callback('get it?');
};
console.log(usingItNow(myCallback));
This has nothing to do with callback. You are printing out the result of a function call, but the usingItNow()
function doesn't return anything, so the result is undefined
.
To prove that it has nothing to do with callback, try to print out the call to a standard function that returns nothing and you will get undefined
too:
function usingItNow(num) {
num = num + num;
};
console.log(usingItNow(10));
Now, simply make the function return something and you will get it printed out:
function usingItNow(num) {
return num + num;
};
console.log(usingItNow(10));
Which leads to what Suren mentioned in his answer, return the result of the callback to get it printed out:
const myCallback = function(data) {
return 'got data: ' + data;
};
const usingItNow = function(callback) {
return callback('get it?');
};
console.log(usingItNow(myCallback));
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
I have a set of radio buttons which are set using the below methodsAt the point of handleChange I'm casting the value to a integer if the target element is a radio button
As the title suggests, I do need to grab a button click from a user on a webpage, and use it to toggle a write on a file hosted on the server
I'm trying to use autobahnjs with ratchet