Skip to content

Commit cc2fea6

Browse files
authored
Add files via upload
1 parent d53e1eb commit cc2fea6

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-0
lines changed

053-Callback-Hell.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Callback hell is a phenomenon where a Callback is called inside another Callback. It is the nesting of multiple Callbacks inside a function. If you look at the design of the code, it seems just like a pyramid. Thus the Callback hell is also referred to as the ‘Pyramid of Doom’.
5+
6+
Callback hell structurally is just a nesting of function calls inside a function. But, it becomes difficult to understand and keep track of the nesting once the size of the nesting is increased.
7+
*/
8+
9+
function print(i){
10+
console.log("This is call number "+i);
11+
}
12+
13+
function fun(callback){
14+
15+
setTimeout(()=>{
16+
17+
let i = 1 ;
18+
callback(i); i++ ;
19+
setTimeout(()=>{
20+
21+
callback(i); i++;
22+
setTimeout(()=>{
23+
24+
callback(i); i++ ;
25+
setTimeout(()=>{
26+
27+
callback(i); i++ ;
28+
setTimeout(()=>{
29+
30+
callback(i); i++ ;
31+
// .... and so on
32+
33+
}, 800)
34+
}, 700)
35+
}, 500)
36+
}, 300)
37+
}, 100)
38+
}
39+
40+
fun(print);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Common JS :
5+
module.exports : to export object.
6+
7+
*/
8+
9+
10+
const PI = 3.14
11+
12+
const add = (a,b) => {
13+
console.log('Add : ',(a+b));
14+
}
15+
16+
const sub = (a,b) => {
17+
console.log('Sub : ',(a-b));
18+
}
19+
const mul = (a,b) => {
20+
console.log('Mul : ',(a*b));
21+
}
22+
const div = (a,b) => {
23+
console.log('Div : ',(a/b));
24+
}
25+
26+
const info = (str = '')=>{
27+
console.log('Calculator....!', str);
28+
}
29+
30+
//export as function
31+
module.exports = {PI, add, subtract : sub,mul};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Common JS :
5+
require('Filepath') : to get exported object
6+
*/
7+
8+
// require('path') to import
9+
const operation = require('./Arithmetic.js');
10+
11+
console.log(operation.PI);
12+
operation.add(2,6);
13+
operation.subtract(10,7);
14+
operation.mul(4,3);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
/*
3+
author : Jaydatt Patel
4+
5+
ES6 Module :
6+
7+
File Name must be with .mjs extension :
8+
moduleFile.mjs
9+
MainFile.mjs
10+
*/
11+
12+
// Named inline Export Syntax:
13+
export const add = (a,b) => {
14+
console.log('Add : ',(a+b));
15+
}
16+
const sub = (a,b) => {
17+
console.log('Sub : ',(a-b));
18+
}
19+
const mul = (a,b) => {
20+
console.log('Mul : ',(a*b));
21+
}
22+
const div = (a,b) => {
23+
console.log('Div : ',(a/b));
24+
}
25+
26+
const info = (str = '')=>{
27+
console.log('Calculator....!', str);
28+
}
29+
30+
//multiple named export
31+
export {sub,mul};
32+
33+
//default single export
34+
export default info;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
ES6 Module :
5+
Modules are a fundamental concept in modern JavaScript development, allowing developers to organize code into separate files or modules. Each module encapsulates its variables, functions, and classes, avoiding naming conflicts and polluting the global scope. This modularity promotes code reusability, maintainability, and scalability in large-scale projects.
6+
7+
File Name must be with .mjs extension :
8+
moduleFile.mjs
9+
MainFile.mjs
10+
11+
*/
12+
13+
//single import
14+
// import {add} from './Arithmetic.mjs';
15+
16+
//multiple import
17+
// import {add,sub} from './Arithmetic.mjs';
18+
19+
//multiple import with alise name
20+
// import {add as addition, sub as subtraction} from './Arithmetic.mjs';
21+
22+
//import default only without {}
23+
// import info from './Arithmetic.mjs';
24+
25+
//import default only with alise name and without {}
26+
// import infomation from './Arithmetic.mjs';
27+
28+
//default and multiple import
29+
// import info, {add,sub} from './Arithmetic.mjs';
30+
31+
//default and multiple import with alise name
32+
// import infomation, {add as additon, sub as subtraction} from './Arithmetic.mjs';
33+
34+
//all import
35+
36+
// require('path') to import
37+
import * as operation from './Arithmetic.mjs';
38+
39+
operation.add(2,3);
40+
operation.sub(10,7);
41+
operation.mul(4,3);
42+
// operation.div(20,2); //error: not fun. need to export to use
43+
operation.default();
44+

0 commit comments

Comments
 (0)