-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.html
80 lines (74 loc) · 2.97 KB
/
example.html
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
<html lang="en">
<head>
<script src="./PromiseChain.js" id=""></script>
</head>
<body>
<script>
var dwarf = {
gold: 3,
money: 0,
digSomeGold: function (scope) {
var me = this;
return new Promise(function (resolve, reject) {
setTimeout(function () {
var goldExtracted = Math.floor(Math.random() * 4);
me.gold += goldExtracted;
// console.log(me);
console.log("The dwarf got " + goldExtracted + " gold, Total [" + me.gold + "]");
resolve(goldExtracted);
}, 1000);
});
},
askPermisionToSell: function (scope) {
var me = this;
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(true);
}, 1000);
});
},
sellGold: function (scope) {
var me = this;
return new Promise(function (resolve, reject) {
setTimeout(function () {
me.gold -= 1;
me.money += 100;
console.log("The dwarf have sell one gold for 100 money");
resolve(me.gold);
}, 1000);
});
},
hasPermision: function (scope) {
var me = this;
return scope.permision === true;
},
hasGold: function (scope) {
var me = this;
console.log("The dwarf have [" + me.gold + "] gold");
return me.gold > 0;
},
countMoney: function (scope) {
var me = this;
return new Promise(function (resolve, reject) {
setTimeout(function () {
console.log("The dwarf have earned " + me.money + " of money");
// console.log(scope);
resolve(me.money);
}, 1000);
});
}
}
new PromiseChain({}, dwarf)
.continue(dwarf.digSomeGold, "fisrtMiningDone")
.continue(dwarf.digSomeGold, "secondMiningDone")
.continue(dwarf.askPermisionToSell, "permision")
.ifElse(dwarf.hasPermision, function (sc) {
return new PromiseChain(sc, dwarf)
.while(dwarf.hasGold, dwarf.sellGold, 'whileResults') // results are stored in an array in the scope if no need you can omit the parameter
.end();
}, function (sc) { return console.warn('go sell your dirty gold anywhere else!'); }, 'ifelseResult')
.continue(dwarf.countMoney)
.end();
</script>
</body>
</html>