-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecAnalysis_And_PseudoCodeFormulation.txt
211 lines (137 loc) · 6.79 KB
/
SpecAnalysis_And_PseudoCodeFormulation.txt
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
Promise A+ Spec-Analysis & Pseudo Code formulation :
Spec URL:https://promisesaplus.com/
A) Analysis:
2.2 The "then" model:
"2.2.6 then may be called multiple times on the same promise.
If/when promise is fulfilled, all respective onFulfilled callbacks must execute in the order of their originating calls to then.
If/when promise is rejected, all respective onRejected callbacks must execute in the order of their originating calls to then"
"2.2.7 then must return a promise ..."
Q: From 2.2.6, if then is called multiple times on same promise, then From 2.2.7, doesn't that means we are creating new promise everytime,
since then returns a promise as well,apart from registering callbacks?
So didn't quite get from 2.2.6,"all respective onFulfilled callbacks must execute in the order of their originating calls" completely
i.e. for 2.2.6 to make sense, there should be an internal array/queue as well for each promise for registering all the then callballbacks in order.
If that is the case, what value does the new promise (or promises?) returned gets, how it's get evaluated based on all these different callbacks [onResolve1,onResolve2..]?
does that means it gets resolved in sequence?
ToDo:Revisit on this post covering basic flow of then() & constructor.
2.3 The resolution procedure [[Resolve]](promise,x) :
"If x is a thenable, it attempts to make promise adopt the state of x"
Q: Adopting the state is understandable if its' a promise, but if it's simply a thenable(i.e. object with then function)
then most probably that is all it would have, i.e. no state vars only a "then" function, so how to deduce it's state in that case?
Maybe the next line "under the assumption that x behaves at least somewhat like a promise." is on the same lines.
ie. we can only adopt it's state if it's a promise(a thenable)! but what to do if it's not a promise? ToDo: Refer 2.3.3
[[Resolve]](promise,x) Steps:
i.e. promise gets input x
2.3.1 if x===promise -> Reject with TypeError
2.3.2 else if x is a typeOf promise -> adopt
2.3.3 else if x is a typeOf object/fn -> {
//if x is object & "then" is a property
retrieve x.then
// if Error e while retrieving, reject(e)->promise
// if Successfully retrieved with value v, resolve(v)->promise (Assumption, not part of spec step)
//if x is function which further contains "then" function
call x.then()
with this=x & (resolvePromise,rejectPromise) callbacks as 1st and 2nd args
// so when, resolvePromise is called with value v, then [[Resolve]](promise,v)
// & when rejectPromise is called with reason e, reject promise with reason e
// ignore any subsequent calls to resolvePromise & rejectPromise
Q: From where does (resolvePromise,rejectPromise) come from & how do they get invoked?
}
2.3.4 else -> fulfill promise with x // We'll start the pseudo-code from here, being the simplest of all
B) Pseudo-Code :
onFulfilledArray:[]
onRejectedArray:[]
status=PENDING,value=undefined,reason=undefined
Promise then(onFulfilled, onRejected){
if(onFulfilled typeOf function) onFulFilledArray.add(onFulfilled)
if(onRejected typeOf function) onRejectedArray.add(onRejected)
??? how to return new promise since it needs to be returned immediately
??? return new Promise(executor)
//noob attempt..
return new Promise(function(resolve,reject){
resolve(onFulfilled) ???
reject(onRejected) ???
});
}
/*other important fns which will be required, not covered in spec*/
constructor()
resolve()
reject()
Promise(executor){
//when executor completes, call:
//if Success
-- set value
-- call resolve(value)
//if Error
-- set reason
-- call reject(reason)
}
resolve(value){
-- changes status to FULFILLED
-- call onFulfilledArray[0] ...
}
reject(reason){
-- changes status to REJECTED
-- call onRejectedArray[0] ...
}
//Attemp#2 something like..
Promise{
onFulfillArr=[];
onRejectArr=[];
status=0;
value;
reason;
promiseArr=[];
//takes a function def. with resolve,reject as args name
Promise(f){
f();//executes that fn , assuming it calls the resolve,reject internally accordingly.
}
resolve(v){
status=1;
value=v;
onFulfillArr[0](v);
//Case 1: when v is a simple value
//Now, we would like to do something like promise.resolve(onFulfillArr[0](v))
//on the promise we returned in then() method.
//how does this hooks with that promise value?
//1 way if we store ref. of all the promises we are creating in then
//promiseArr[0].then(onFulfillArr[0](v)); //??? This doesn't looks cool also, then won't do anything apart from registering the callback
// main challenge is in thinking about it's executor at the first place when returning from then()
//Case 2: when v is object/promise case..
// we apply promise resolution procedure steps [[Resolve]](promise,v)
}
reject(r){
status=2;
reason=r;
onRejectArr[0](r);
}
Promise then(onFulfill,onReject){
if(onFulfill typeOf function) onFulfillArr = [ ...onFulfillArr, onFulfill];
if(onReject typeOf function) onRejectArr =[...onRejectArr , onReject];
return Promise(f(onResolve,onReject){
}); // ???
//not sure on this!! what would be it's correct executor fn like?
}
**The Aha-moment"
val="1234";
reason="TypeError";
// var promise = new Promise(()=> ( resolve => (val =>console.log("I'm resolved")) ,
// reject => (reason => console.log("I'm rejected"))
// ));
// Thanks to playing it out in actual code editor and below piece of info from
// https://codeburst.io/a-simple-guide-to-es6-promises-d71bacd2e13a
// const delay = (ms) => new Promise(
// (resolve) => setTimeout(resolve, ms)
// );
//THIS is it!! & I was not aware about this technique at all(duh!).
// So, In this example, we are using a function to wrap our promise so that it does not execute immediately.
//make it a function to evaluate the same on-demand i.e. when values are passed
var p = (value,reason) => new Promise(function( resolve,reject){
if(value) resolve("I'm resolved with value: " + value);
else reject("I'm rejected :( with reason: " + reason);
}
);
//we have the ref. and we can pass it around in our code acc..
//for eg: keeping collection of all such references.
p(val,reason) // <-- actually invoke it now once we have the val and reason objects. Yes! & it works
.then((v)=>console.log(":) " + v))
.catch((e)=>console.log(":( " + e));