generated from CodecoolArchive/async-workshop-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromises.js
More file actions
38 lines (34 loc) · 947 Bytes
/
promises.js
File metadata and controls
38 lines (34 loc) · 947 Bytes
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
const random = (max) => Math.floor(Math.random() * (max + 1))
export const sleep = (waitingTimeMs) => {
return new Promise((fulfill, reject) => {
setTimeout(() => {
fulfill()
}, waitingTimeMs)
})
}
export const getRandomNumber = () => {
return new Promise((fulfill, reject) => {
setTimeout(() => {
const max = 10;
fulfill(random(max))
}, 0)
})
}
export const openSchrodringerBox = () => {
return new Promise((fulfill, reject) => {
setTimeout(() => {
const isAlive = Math.random() > 0.5
const catNames = ['Cirmi', 'Butyok', 'Kormi', 'Fluffy']
const selectedCat = catNames[random(catNames.length - 1)]
if (isAlive) {
fulfill(selectedCat)
} else {
reject(new Error(`Ooops, right now ${selectedCat} has only 8 lives.`))
}
}, 0)
})
}
export const getRandomNumberSlowly = async () => {
await sleep(2000);
return await getRandomNumber();
}