-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.js
64 lines (54 loc) · 1.4 KB
/
day6.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
TOPICS:
1.Callbacks
2.SetTimeout
3.SetInterval
4.async/sync concepts
5.promises
6.promises handling using them/catch
7.promises handling using async/await
*/
// 1.Callbacks
// A function calling another function by getting the function in its parameters
function foo() {
console.log("foo");
}
function bar(cb) {
console.log("bar");
cb();
}
bar(foo);
// understanding callbacks through setTimeout and setInterval
// 2.setTimeout(callback,time(in ms))
setTimeout(() => {
console.log("I will get printed after 5 seconds only!");
}, 5000);
// 3.setInterval(callback,time(in ms))
setInterval(() => {
console.log("I will get printed repeatedly after a interval of 2 seconds!");
}, 2000);
/*
4.sync/async
in synchronous js a operation takes place one at a after the completion of previous task
in asynchronous js more than one operation can take place simultaneously without having to wait for previous task to be completed
*/
// 5.Promises
// promise creation
// let obj1 = new Promise(callback(resolve,reject))
let obj1 = new Promise((resolve, reject) => {
setTimeout(() => resolve("I will give my response after 5 sec!"), 5000);
});
// a) promise handling using then and catch
obj1
.then((e) => {
console.log(e);
})
.catch((err) => {
console.log(err);
});
// b) promise handling using async/await
async function handler() {
const response = await obj1;
console.log(response);
}
handler();