-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-callbacks.js
More file actions
33 lines (28 loc) · 817 Bytes
/
2-callbacks.js
File metadata and controls
33 lines (28 loc) · 817 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
//Callbacks are the foundation of Node.js
//A callback is a function called at the completion of a given task;
//This prevents any blocking, and allows other code to be run in the meantime.
const doWorkCallback = callback => {
setTimeout(() => {
callback("This is an error!", undefined);
//callback(undefined, [1,2,3])
}, 2000);
};
doWorkCallback((error, result) => {
if (error) {
return console.log(error);
}
console.log(result);
});
//Previous Sintaxe
function myFunction(myCallback) {
setTimeout(function anotherFunction() {
//callback('This is an error!', undefined)
myCallback("error", undefined);
}, 2000);
}
myFunction(function oneMoreFunction(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});