Skip to content

Commit 2478b40

Browse files
committed
Callbacks Promises Async Await
1 parent eedbaf5 commit 2478b40

10 files changed

Lines changed: 303 additions & 0 deletions

File tree

670 KB
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Sync in JS
3+
=============
4+
Synchronous means the code runs in a particular sequence of instructions given in the program.
5+
Each instruction waits for the previous instruction to complete its excution.
6+
*/
7+
8+
console.log("One");
9+
console.log("Two");
10+
console.log("Three");
11+
/*
12+
ASynchronous
13+
===============
14+
Due to Synchronous Programming,sometimes imp instructions
15+
get blocked due to some previous instrctions,which cause delay
16+
in the UI . ASynchronous code execution allows to execute next
17+
instruction immediately and does not block the flow.
18+
*/
19+
console.log("One");
20+
console.log("Two");
21+
setTimeout(()=>{
22+
console.log("hello");
23+
},4000)// timeout 4s=4000ms
24+
console.log("Three");
25+
console.log("Four");
26+
27+
28+
/*
29+
Callbacks-A callback is a funtion passed as an argument to another funtion
30+
*/
31+
function sum(a ,b){
32+
console.log(a+b);
33+
}
34+
35+
function calculator(a,b,sumCallback){
36+
sumCallback(a,b);
37+
}
38+
/*
39+
here sum is a funtion that passed
40+
as a argument in calculator funtion
41+
*/
42+
calculator(1,2,sum);
43+
44+
45+
46+
47+
48+
49+
50+
/*
51+
52+
*/
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
/*
3+
Callback hell Example
4+
Callback hell-Nested Callback stacked below one another formimg
5+
pyramid structure
6+
*/
7+
function getData(dataId,getNextData){
8+
setTimeout(()=>{
9+
console.log("data",dataId);
10+
if(getNextData){
11+
getNextData();
12+
}
13+
14+
},2000);
15+
}
16+
17+
getData(1,()=>{
18+
getData(2,()=>{
19+
getData(3);
20+
});
21+
})
22+
23+
24+
/*
25+
Promises:-Callback hell is complex to understand thats why Promises are used
26+
Promise is eventual task.It is an object in js . it is a soloution of
27+
callback hell.
28+
29+
*/
30+
//pending
31+
let promise2=new Promise((resolve,reject)=>{
32+
console.log("I am a pending promisses");
33+
});
34+
//resovle
35+
let promise=new Promise((resolve,reject)=>{
36+
console.log("I am a resolve promisses");
37+
resolve("success");
38+
39+
});
40+
41+
//reject
42+
let promise1=new Promise((resolve,reject)=>{
43+
console.log("I am a reject promisses");
44+
reject("some error");
45+
});
46+
47+
48+
/*
49+
Example of Promises
50+
*/
51+
52+
function GetData(id,nextdata){
53+
return new Promise((resolve,reject)=>{
54+
setTimeout(()=>{
55+
console.log("data",id);
56+
resolve("success");
57+
if(nextdata){
58+
nextdata();
59+
}
60+
},10000);
61+
} );
62+
63+
}
64+
65+
const getPromises=()=>{
66+
return new Promise((resolve,reject)=>{
67+
console.log("I am a promises");
68+
resolve("success");
69+
//reject("error")
70+
});
71+
};
72+
73+
let val=getPromises();
74+
val.then((res)=>{
75+
console.log("Promise Fulfilled",res);
76+
});
77+
78+
val.catch((err)=>{
79+
console.log("rejected",err);
80+
})
81+
82+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//promise Chaining
2+
function asyncFunc1(){
3+
return new Promise((resolve,reject)=>{
4+
setTimeout(()=>{
5+
console.log(" data 1");
6+
resolve("Success Data");
7+
},4000)
8+
});
9+
}
10+
11+
function asyncFunc2(){
12+
return new Promise((resolve,reject)=>{
13+
setTimeout(()=>{
14+
console.log(" data 2");
15+
resolve("Success Data");
16+
},4000)
17+
});
18+
}
19+
console.log("Fetching Data 1...........");
20+
asyncFunc1().then((res)=>{
21+
console.log(res);
22+
console.log("Fetching Data 2...........");
23+
asyncFunc2().then((res)=>{
24+
console.log(res);
25+
});
26+
27+
});
28+
29+
30+
//promise chaining example
31+
function GetData(id,nextdata){
32+
return new Promise((resolve,reject)=>{
33+
setTimeout(()=>{
34+
console.log("data",id);
35+
resolve("success");
36+
37+
},10000);
38+
});
39+
}
40+
41+
GetData(1).
42+
then((res)=>{
43+
return GetData(2);
44+
})
45+
.then((res)=>{
46+
return GetData(3);
47+
})
48+
.then((res)=>{
49+
console.log(res);
50+
})
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
/*
3+
Async-Await
4+
--------------
5+
async funtion always returns promise
6+
await pauses the execution of its surroundings async funtion until
7+
the promise is settled.
8+
9+
*/
10+
11+
12+
async function hello(){
13+
console.log("Hello");
14+
}
15+
16+
17+
18+
function api(){
19+
return new Promise((resolve,reject)=>{
20+
setTimeout(()=>{
21+
console.log("Weather Data");
22+
resolve(200);
23+
},5000)
24+
});
25+
}
26+
27+
async function getweatherData(){
28+
await api();//1st call
29+
await api();//2nd call
30+
}
31+
32+
33+
34+
//Async-await Example
35+
function GetData(id){
36+
return new Promise((resolve,reject)=>{
37+
setTimeout(()=>{
38+
console.log("data",id);
39+
resolve("success");
40+
41+
},5000);
42+
});
43+
}
44+
45+
//Async-Await
46+
async function GetAllData(){
47+
console.log("Getting data 1....")
48+
await GetData(1);
49+
console.log("Getting data 2....")
50+
await GetData(2);
51+
console.log("Getting data 3....")
52+
await GetData(3);
53+
}
54+
55+
/*
56+
IIFE-Immediately invoked Function Expression
57+
IIFE is a function that is called immediately invoked as soon as it is defined
58+
*/
59+
60+
(async function (){
61+
console.log("Getting data 1....")
62+
await GetData(1);
63+
console.log("Getting data 2....")
64+
await GetData(2);
65+
console.log("Getting data 3....")
66+
await GetData(3);
67+
})();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Synchronous,ASynchronous,Callbacks</title>
7+
</head>
8+
<body>
9+
<script src="app.js"></script>
10+
</body>
11+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Callback hell Example,Promises</title>
7+
</head>
8+
<body>
9+
10+
<script src="app1.js"></script>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Promisses Chaining</title>
7+
</head>
8+
<body>
9+
10+
<script src="app2.js"></script>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Async Await</title>
7+
</head>
8+
<body>
9+
10+
<script src="app3.js"></script>
11+
</body>
12+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"bracketpaircolordlw.bracket-pair-color-dlw"
4+
]
5+
}

0 commit comments

Comments
 (0)