0
Someone to ask questions about callback?
How is it really a callback? Callback is it? Code: function exe(callback) { console.log("test") callback(); } function ex() { console.log("test2") } exe(ex);
6 Antworten
+ 1
Yes, in this case `res` is the callback.
+ 2
Not totally sure what you're asking, but the `exe` function, instead of returning a useful value, calls a function once it's done.
That function is called a callback, or outside of javascript-land, a continuation.
Note that all you are really doing is
console.log("test");
console.log("test2");
So continuation-passing style (cps) is just another way to structure code and you can (*usually) convert between direct style and cps freely.
__
*In javascript it's not that simple because of asynchronous code, like code that runs when you click a button on a web page.
In nodejs you will see callbacks everywhere.
+ 1
Here's one you might know:
setTimeout(function(){
console.log("Hello!");
}, 2000);
This will print "Hello!" in two seconds. The function you are passing is the callback.
Here's one from nodejs:
fs.readFile("file.txt", (err, data) => {
console.log("file.txt contains:", data);
});
This will execute the callback once nodejs is done reading the file.
+ 1
Schindlabua so I see well if I understand, because I'm kind of lost kkk, and if I do like this? Can it be considered a callback?
ex:
var param = {
name: 'Doug',
email: 'blabla@blabla.com.br'
};
exe(param, res);
function res(resposta) {
alert(resposta);
}
function exe(para, callback) {
console.log("ok");
data = para.name;
callback(data);
}
0
Schindlabua can you show me an easy example of a callback, so I'm having trouble understanding?
0
Schindlabua thank you