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);

31st May 2019, 7:58 AM
Matheus Batista
Matheus Batista - avatar
6 Antworten
+ 1
Yes, in this case `res` is the callback.
31st May 2019, 10:11 PM
Schindlabua
Schindlabua - avatar
+ 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.
31st May 2019, 10:23 AM
Schindlabua
Schindlabua - avatar
+ 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.
31st May 2019, 9:36 PM
Schindlabua
Schindlabua - avatar
+ 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); }
31st May 2019, 9:54 PM
Matheus Batista
Matheus Batista - avatar
0
Schindlabua can you show me an easy example of a callback, so I'm having trouble understanding?
31st May 2019, 2:48 PM
Matheus Batista
Matheus Batista - avatar
0
Schindlabua thank you
31st May 2019, 10:14 PM
Matheus Batista
Matheus Batista - avatar