forked from johnandblue/async-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.js
57 lines (48 loc) · 1.43 KB
/
async.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
// log the `rnd` result in the console using all three async techniques.
// You can only call console.log inside the `main` function.
const randomNumber = () => {
return Math.random();
}
// 1. Make it wait for 1 sec. with `setTimeout` and log it on main function
const timeoutRandomNumber = (cb) => {
setTimeout(() => {
try {
cb(null,randomNumber());
} catch (err) {
cb(err);
}
},1000);
}
// 2. Now wrap the timeout version to work with promises
const promiseRandomNumber = () => {
return new Promise(function(resolve,reject) {
timeoutRandomNumber((err,num) => {
if (err) reject(err);
else resolve(num);
})
});
}
// 3. Finally, code a final version with async await.
const asyncRandomNumber = async () => {
return await promiseRandomNumber();
}
const rangedRandomNumber = (base, min, max) => {
return Math.floor((base * (max - min)) + min);
}
const main = () => {
const rnd = randomNumber();
console.log('Random ',rangedRandomNumber(rnd, 14, 42));
// log rnd…
timeoutRandomNumber((err,rnd)=>{
if (err) throw err;
else console.log('Callback ',rangedRandomNumber(rnd, 14, 42));
});
promiseRandomNumber()
.then(rnd => console.log('Promise ',rangedRandomNumber(rnd, 14, 42)))
.catch(err => console.log(err));
asyncRandomNumber()
.then(rnd => console.log('Async ',rangedRandomNumber(rnd, 14, 42)))
.catch(err => console.log(err));
}
main();
module.exports = main;