-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13_promises.js
72 lines (52 loc) · 2.26 KB
/
13_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*-----------------------------------------------------------------*/
/* A promise is an object that will return a value in future (resolved or rejected). It has three states: pending, fulfilled or rejected.
// Because of this “in future” thing, Promises are well suited for asynchronous JavaScript operations.
// Great article on Promises -> https://zellwk.com/blog/js-promises/
-------------------------------------------------------------------*/
// NOTE: resolve() and reject() are like conductors => they pass the value. Therefore they are ideal to serve as callbacks.
function test() {
return new Promise(function (resolve, reject) {
setTimeout(() => {
let randomNum = Math.random();
if (randomNum >= 0.5) {
resolve(`Resolved 🔥 ${randomNum}`)
} else{
reject(`Rejected 😩`)
}
}, 2000);
})
}
test()
.then(res => console.log(res)) // Grabbing resolve
.catch(err => console.log(err)) // Grabbing reject
// Example. I am preparing a party. Sofia promised to buy a Neapoletano cake in a week (1 sec in our case).
const sofiaBuysCake = cakeType => {
return new Promise((resolve, reject) => {
setTimeout(function(){
if (cakeType === 'Neapoletano') {
resolve("Let's partyyyyy!")
} else {
reject("Nope. Wrong cake.")
}
}, 1000)
})
}
function buyMyself() {
console.log('I will buy it myself.');
}
sofiaBuysCake('neapoletan')
.then(response => console.log(response))
.catch(error => {
console.log(error)
buyMyself()
})
// That is the way promises work in JS, they are used to get or modify a piece of information.
// When the promise resolves, we do something with the data that comes back.
// When the promise rejects, we handle the error.
/*-----------------------------------------------------------------*/
/* There are three reasons why developers prefer promises over callbacks:
//
// Promises reduce the amount of nested code.
// Promises allow the visualization of the execution flow easily.
// Promises allows handling all the errors at once at the end of the chain.
-------------------------------------------------------------------*/