-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-async-(type:standard).js
100 lines (79 loc) · 2.75 KB
/
class-async-(type:standard).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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* --- fake async task */
function simulateTask(delay, str) {
return new Promise((resolve) =>
setTimeout(() => {
resolve(str);
}, delay)
);
}
/* --- async class */
class myClass {
constructor() {
this.data = {}
this.unveil = (obj) => {
return obj ? JSON.parse(JSON.stringify(obj)) : 'unveil requires an object or an array'
}
}
async init() {
return Promise.all([
this.produceTheFirstDataAsset(),
this.produceTheSecondDataAsset('firsttest ')
])
}
async produceTheFirstDataAsset() {
//do operations
let stack = await Promise.all([
simulateTask(250, `task 1.1 completed`),
simulateTask(500, `task 1.2 completed`),
simulateTask(250, `task 1.3 completed`),
])
//save result
this.data['TEST01'] = [stack[ 0 ], stack[ 1 ], stack[ 2 ]]
console.log('the first asset was produced')
//return result
return this.data
}
async produceTheSecondDataAsset(str) {
//do operations
let stack = await Promise.all([
simulateTask(250, `task ` + str + `2.1 completed`),
simulateTask(500, `task ` + str + `2.2 completed`),
simulateTask(250, `task ` + str + `2.3 completed`),
])
//save result
this.data['TEST02'] = [stack[ 0 ], stack[ 1 ], stack[ 2 ]]
console.log('the second asset was produced')
//return result
return this.data
}
}
async function initMyClass() {
let myClassIstance = new myClass()
await myClassIstance.init()
return myClassIstance
}
/*export*/ const myClassIstance = initMyClass()
// how to use:
//
// > if standard:
//
// <script src="script.js"></script>
// <script>
// myClassIstance.then( async classAsset => {
// console.log('the init test data:', unveil(classAsset) )
// if(classAsset.data['TEST02'][0]=="task firsttest 2.1 completed"){
// classAsset.produceTheSecondDataAsset('rewritten ')
// console.log('the refreshed data:', unveil(classAsset) )
// }
// })
// </script>
//
// unveil method note:
// If you try to print an object in console and then edit it, that
// object will only appear as in the latest update. Hence, you can
// no longer read the previous status. It is a purely visual phenomenon
// (the state, if called by navigating the object, is then legible).
// This is a known browser debugging bug. To work around you can use the
// unveil (obj) method. Without it, you will only be able to see your
// latest status. Exemple: console.log( classAsset.unveil(classAsset) ).
// read more on: https://stackoverflow.com/q/4057440/19579604