-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.js
More file actions
38 lines (30 loc) · 797 Bytes
/
callback.js
File metadata and controls
38 lines (30 loc) · 797 Bytes
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
27
28
29
30
31
32
33
34
35
36
/*
What is callback?
- Its just a mechanism to tell a function to call back when its done with its processing.
- And no callbacks are not asynchronous by themselves.
*/
// Example 1
function doAsyncTask(cb) {
cb();
}
doAsyncTask(() => {console.log(message)});
var message = 'Carolina Marine';
// Example 2
function doAsyncTask2(cb) {
setTimeout(cb, 0);
}
doAsyncTask2(() => {console.log(message2)});
var message2 = 'Anthony Ginting here!';
// Example 3
function doAsyncTask3(cb) {
setTimeout(() => {cb(null, message3)}, 0);
}
// Error first callback, always have the first param as error.
doAsyncTask3((err, data) => {
if(err) {
throw err;
} else {
console.log('data: ', data);
}
});
var message3 = 'Tai Tzu Ying is the best!';