-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2a-promise.js
39 lines (30 loc) · 1011 Bytes
/
lab2a-promise.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
///// Problem 1 - Fun with Promises
function flip() {
let p = new Promise(function (resolve, reject) {
if (Math.random() > 0.5) resolve();
else reject();
});
return p;
}
//// Add code here that will "flip" the coin ten times and write the
//// result to the console (e.g. "Heads" or "Tails" for each flip).
for (let i = 0; i < 10; i++) {
flip().then(() => console.log("Heads!"), () => console.log("Tails!"));
}
///// Problem 2 - More fun...
function countBig(bignum) {
// Add code here that returns a Promise that will resolve after it has counted to bignum
return new Promise((resolve, reject) => {
for (let i = 0; i < bignum; i++) {
// just counting here...
}
resolve();
})
}
start = Date.now();
bignum = 1000000000;
countBig(1000000000).then(()=> {
console.log("It took " + (Date.now() - start) + " ms to count to " + bignum);
}, () => {
console.log("A problem occurred while trying to count to " + bignum);
})