forked from roerohan/Miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample2.js
More file actions
50 lines (39 loc) · 1.65 KB
/
sample2.js
File metadata and controls
50 lines (39 loc) · 1.65 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Sample javascript program, callbacks.
// Comment out all other sections before executing one section.
// Section One
function slowFunction(num) {
setTimeout( () => { // This is a callback function(A function passed inside another)
console.log(`You entered ${num}.`);
console.log("This took 2 seconds");
}, 2000);
}
slowFunction(10);
console.log("This gets printed first."); // This line gets printed first, even though it is typed later.
//This can be made to function in order with callbacks.
// // Section Two
// // Creating a function with a callback function. The callback is a function, like any other function.
// // In JS you can pass a function as a parameter.
// function slowerFunc(num, callback) {
// console.log("Waiting for 2.5 seconds.");
// setTimeout( () => {
// console.log(`You entered ${num}`);
// callback(); // Execute the function which was passed as callback.
// }, 2500);
// }
// slowerFunc(15, function () { // Passing a nameless function, alternative: slowerFunc(10, () => {
// console.log("This gets printed afterwards because this function passed as a callback.")
// });
// // Section Three
// // You can pass any function as a callback
// function someFunc(num, callback) {
// console.log("Waiting for 3 seconds.");
// setTimeout( function () { // Passing a nameless function [ same as () => { ]
// console.log(`You entered ${num}`);
// callback(); // Execute the function which was passed as callback.
// }, 3000);
// }
// function passAsCallback() {
// var a = "callback";
// console.log(`This is the ${a} function`);
// }
// someFunc(20, passAsCallback);