Skip to content

Commit 2623980

Browse files
authored
Add files via upload
1 parent 9719651 commit 2623980

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

051-Promise.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
author : Jaydatt Patel
3+
4+
Promises : The Promise object represents the eventual completion or failure of an asynchronous operation and its resulting value.
5+
6+
constructor :
7+
new Promise( (resolveFun, rejectFun) =>{});
8+
9+
A Promise is in one of these states:
10+
- pending: initial state, neither fulfilled nor rejected.
11+
- fulfilled: meaning that the operation was completed successfully.
12+
- rejected: meaning that the operation failed.
13+
14+
The .then() method takes up to two arguments; the first argument is a callback function for the fulfilled case of the promise, and the second argument is a callback function for the rejected case. Each .then() returns a newly generated promise object, which can optionally be used for chaining;
15+
16+
Handling a rejected promise in each .then() has consequences further down the promise chain. Sometimes there is no choice, because an error must be handled immediately. In such cases we must throw an error of some type to maintain error state down the chain. On the other hand, in the absence of an immediate need, it is simpler to leave out error handling until a final .catch() statement. A .catch() is really just a .then() without a slot for a callback function for the case when the promise is fulfilled.
17+
18+
- Promise.all() : Fulfills when all of the promises fulfill; rejects when any of the promises rejects.
19+
20+
- Promise.allSettled() : Fulfills when all promises settle.
21+
22+
- Promise.any() : Fulfills when any of the promises fulfills; rejects when all of the promises reject.
23+
24+
- Promise.race() : Settles when any of the promises settles. In other words, fulfills when any of the promises fulfills; rejects when any of the promises rejects.
25+
26+
*/
27+
28+
//--------------------Sample-1--------------------
29+
let request = new Promise( (resolve, reject) =>{
30+
31+
setTimeout(()=>{
32+
console.log(`----------Sample-1-----------`);
33+
console.log(`Promise initiated`);
34+
let x = 0;
35+
if(x === 0){
36+
resolve('Request resolved...');
37+
}else{
38+
reject('Request rejected...');
39+
}
40+
},100);
41+
42+
} );
43+
44+
request.then((val)=>{
45+
console.log(val);
46+
}).catch((errVal)=>{
47+
console.log(errVal);
48+
})
49+
50+
51+
//--------------------Sample-2--------------------
52+
function getUserData (userId) {
53+
return new Promise((resolve, reject) => {
54+
setTimeout(() => {
55+
console.log(`----------Sample-2-----------`);
56+
if (userId === 123) {
57+
resolve({ id: 123, name: 'John Doe', age: 30 });
58+
} else {
59+
reject('User not found');
60+
}
61+
}, 150);
62+
});
63+
}
64+
65+
function displayUserData (userId) {
66+
getUserData (userId)
67+
.then((userData) => {
68+
console.log('User data:', userData.name);
69+
})
70+
.catch((error) => {
71+
console.error('Error: ', error);
72+
});
73+
}
74+
75+
displayUserData(123);
76+
77+
//--------------------Sample-3--------------------
78+
const promise3 = new Promise((resolve, reject) => {
79+
setTimeout(resolve, 250, 'one');
80+
});
81+
82+
const promise4 = new Promise((resolve, reject) => {
83+
setTimeout(resolve, 200, 'two');
84+
});
85+
86+
Promise.race([promise3, promise4]).then((value) => {
87+
console.log(`----------Sample-3-----------`);
88+
console.log(value);
89+
// Both resolve, but promise2 is faster
90+
});
91+
92+
93+
//--------------------Sample-4--------------------
94+
const promise1 = new Promise((resolve) => setTimeout(() => resolve (1), 3000));
95+
const promise2 = new Promise((resolve) => setTimeout(() => resolve(2), 1000));
96+
97+
Promise.all([promise1, promise2])
98+
.then((results) => {
99+
console.log(`----------Sample-4-----------`);
100+
console.log(results);
101+
});
102+

052-Async-await-function.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Author : Jaydatt Patel
3+
4+
Async function : The async function declaration creates a binding of a new async function to a given name. The await keyword is permitted within the function body, enabling asynchronous, promise-based behavior to be written in a cleaner style and avoiding the need to explicitly configure promise chains.
5+
6+
*/
7+
8+
function hold() {
9+
return new Promise((resolve) => {
10+
console.log('On Hold');
11+
setTimeout(() => {
12+
resolve('resolved');
13+
}, 2000);
14+
});
15+
}
16+
17+
async function asyncCall() {
18+
console.log('calling');
19+
const result = await hold();
20+
console.log(result);
21+
}
22+
23+
asyncCall();
24+
25+
26+
27+

0 commit comments

Comments
 (0)