Skip to content

Commit 18f162d

Browse files
committed
week2 vid1
1 parent e0f22ad commit 18f162d

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

js_notes/cohertnotes/week1/10. async.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,21 @@ console.log("Code ends");
106106
// 2.5) Using .then() we can control when we call the cb(callback) function.
107107

108108
// 3. To avoid callback hell (Pyramid of doom) => We use promise chaining. This way our code expands vertically instead of horizontally. Chaining is done using '.then()'
109-
// 4. A very common mistake that developers do is not returning a value during chaining of promises. Always remember to return a value. This returned value will be used by the next .then()
109+
// 4. A very common mistake that developers do is not returning a value during chaining of promises. Always remember to return a value. This returned value will be used by the next .then()
110+
111+
112+
const inventory = {
113+
sunglass : 120,
114+
pants : 24,
115+
shirts : 40,
116+
}
117+
const stockchecker = (resolve,reject) => {
118+
if(inventory.sunglass > 0){
119+
resolve("Sunglasses are available");
120+
}
121+
else{
122+
reject("Sunglasses are out of stock");
123+
}
124+
}
125+
const mypromise = new Promise(stockchecker);
126+
console.log(mypromise);

js_notes/cohertnotes/week1/promise.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//High level difference between the syntax of asynchronous javascript
2+
//1. Callbacks
3+
//2. Promises
4+
//3. Async / awaits
5+
6+
//without callbacks - function is not passed as a parameter
7+
function square(n){
8+
return n*n;
9+
}
10+
function cube(n){
11+
return n*n*n;
12+
}
13+
function sumofsquare(a,b){
14+
let square1 = square(a);
15+
let square2 = square(b);
16+
return square1+square2;
17+
}
18+
console.log(sumofsquare(3,4));
19+
20+
//using callbacks - function is passed as a parameter
21+
function square(n){
22+
return n*n;
23+
}
24+
function cube(n){
25+
return n*n*n;
26+
}
27+
function ans(a,b,operation){
28+
let x = operation(a);
29+
let y = operation(b);
30+
console.log(x+y);
31+
}
32+
ans(1,2,cube);

0 commit comments

Comments
 (0)