forked from HackYourFuture/JavaScript2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-async.js
More file actions
80 lines (63 loc) · 1.88 KB
/
sync-async.js
File metadata and controls
80 lines (63 loc) · 1.88 KB
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
72
73
74
75
76
77
78
79
80
const greenGrocer = {
buyApples: function(quantity) {
return quantity + ' apples';
},
buyTomatoes: function(quantity) {
return quantity + ' tomatoes';
},
buyCauliflower: function(quantity) {
return quantity + ' cauliflower';
},
};
const cheeseShop = {
buyCheese: function(type, weight) {
this.cutCheese(type, weight);
return weight + 'gr ' + type + ' cheese';
},
cutCheese: function(type, weight) {
const stopTime = Date.now() + 100;
while (Date.now() < stopTime);
},
};
const bakery = {
buyBread: function(breadReadyCallback, type, quantity) {
setTimeout(function() {
breadReadyCallback(quantity + ' ' + type);
}, 1000);
},
};
function visitGreengrocer() {
console.log('=== Greengrocer ===');
const beforeBuy = Date.now();
const fruitsAndVegetables = [
greenGrocer.buyApples(2),
greenGrocer.buyTomatoes(2),
greenGrocer.buyCauliflower(1),
];
const timeTaken = (Date.now() - beforeBuy).toFixed(2);
console.log('You bought: ', fruitsAndVegetables);
console.log('This took: ' + timeTaken + 'ms\n');
}
function visitCheeseShop() {
console.log('=== Cheese Shop ===');
const beforeBuy = Date.now();
const cheese = cheeseShop.buyCheese('belegen', 500);
const timeTaken = (Date.now() - beforeBuy).toFixed(2);
console.log('You bought: ' + cheese + '');
console.log('This took: ' + timeTaken + 'ms\n');
}
function visitBakery() {
console.log('=== Bakery ===');
const beforeBuy = Date.now();
const breadReady = function(bread) {
const timeTaken = (Date.now() - beforeBuy).toFixed(2);
console.log('--- Callback from Bakery ---');
console.log('You bought: ' + bread + '');
console.log('This took: ' + timeTaken + 'ms\n');
};
bakery.buyBread(breadReady, 'fijn volkoren', 1);
console.log(`We'll call you back!\n`);
}
visitGreengrocer(); // ?.
visitBakery(); // ?.
visitCheeseShop(); // ?.