-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.js
41 lines (35 loc) · 1.51 KB
/
promises.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
//* Promises :
//? A Promise is an object representing the eventual completion or failure of an asynchronous operation and its resulting value. Promises are a way to handle asynchronous
//? A promise is an object that may produce a single value sometime in the future.
//? It has three states: pending, fulfilled (with data), and rejected (with error).
const myPromise = new Promise(function (res, rej) {
setTimeout(function () {
if (Math.random() < 0.9) {
return res("Hooray!");
}
return rej("Oh no!");
}, 1000);
});
myPromise
.then(function (data) {
console.log("Success: " + data);
})
.catch(function (err) {
console.log("Error: " + err);
});
//todo ==> If Math.random() returns less than 0.9 the following is logged:// "Success: Hooray!"// If Math.random() returns 0.9 or greater the following is logged:// "Error: On no!"
//todo ==> A Promise is an object representing the eventual completion or failure of an asynchronous operation. Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function
//todo ==> Promises resolve the issue of ‘callback hell’ which is nothing but a recursive structure of callbacks (callbacks within callbacks within callbacks and so forth).
const promise = new Promise((resolve, reject) => {
isNameExist = false;
if (isNameExist) {
resolve("User name exist");
} else {
reject("error");
}
});
promise
.then((result) => console.log(result))
.catch((error) => {
console.log(error);
});